Passing extra arguments through connect

后端 未结 1 1122
闹比i
闹比i 2020-11-22 03:57

Is it possible to pass variables through slots so I can print out certain text? Trying to pass variable \'DiffP\' which is defined in another function to slot.

\'Dif

相关标签:
1条回答
  • 2020-11-22 04:09

    The problem can be solved in 2 ways:

    Using lambda functions:

    In general:

        obj.signal.connect(lambda param1, param2, ..., arg1=val1, arg2= value2, ... : fun(param1, param2,... , arg1, arg2, ....))
    
    def fun(param1, param2,... , arg1, arg2, ....):
        [...]
    

    where:

    • param1, param2, ... : are the parameters sent by the signal
    • arg1, arg2, ...: are the extra parameters that you want to spend

    In your case:

        self.buttonGroup.buttonClicked['int'].connect(lambda i: self.input(i, "text"))
    
    @pyqtSlot(int)
    def input(self, button_or_id, DiffP):
        if isinstance(button_or_id, int):
            if button_or_id == 0:
                self.TotalInput[0].setText(DiffP)
            elif button_or_id == 1:
                self.TotalInput[54].setText('1')
    

    Using functools.partial:

    In general:

        obj.signal.connect(partial(fun, args1, arg2, ... ))
    
    def fun(arg1, arg2, ..., param1, param2, ...):
        [...]
    

    where:

    • param1, param2, ... : are the parameters sent by the signal
    • arg1, arg2, ...: are the extra parameters that you want to send

    In your case:

    from functools import partial
    
        [...]
        self.buttonGroup.buttonClicked['int'].connect(partial(self.input, "text"))
    
    
    @pyqtSlot(int)
    def input(self, DiffP, button_or_id):
        if isinstance(button_or_id, int):
            if button_or_id == 0:
                self.TotalInput[0].setText(DiffP)
            elif button_or_id == 1:
                self.TotalInput[54].setText('1')
    
    0 讨论(0)
提交回复
热议问题