Passing extra arguments through connect

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')

Leave a Comment