'Custom signals and multi-parameter slots

I am using PyQt5 for my project. I have a widget class which returns a custom signal based on some other signal. This signal will connect to a slot, which will use the signal information to do something based on some other parameters. The problem is that I do not know how to include both, the parameters that the signal itself passes, and the ones that I want to include in the slot. Here a toy model (I know it does not make practical sense, but it still shows my case):

class Buttons(QtWidgets.QWidget):

    pressed_button = qtc.pyqtSignal(int)

    def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)

         self.button1 = QtWidgets.QPushButton()
         self.button2 = QtWidgets.QPushButton()

         self.button1.setObjectName('button1')
         self.button2.setObjectName('button2')

         self.button1.clicked.connect(self.pressed_button)
         self.button2.clicked.connect(self.pressed_button)

    def pressed_button(self):
        
        if self.sender().objectName() == 'button1':
           self.pressed_button.emit(1)
        elif self.sender().objectName() == 'button2':
           self.pressed_button.emit(2)

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self, *args, **kwargs):
         super().__init__(*args, **kwargs)

         self.buttons = Buttons()

         self.buttons.pressed_button.connect(self.print_button('Pressed button is '))

    def print_button(self, n, msg):

         print(msg + str(n))

The issue is that if you give the msg = 'Pressed button is ', the pring button function still expects you give variable 'n', when what I want is that this variable comes directly from the emitted signal. How can I solve this?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source