Environment:
Python2.7.8
Pyqt 4.11.1
During pyqt programming, extra parameters are often transferred to the slot function. However, the signal-slot mechanism only specifies how the signal is connected to the slot, and the signal-defined parameters are passed to the slot, while the additional parameters (User-Defined) cannot be passed directly.
It is very useful to pass additional parameters. You may use a single slot to process signals from multiple components and sometimes transmit additional information.
One way is to use a Lambda expression.
from PyQt4.QtCore import *from PyQt4.QtGui import *class MyForm(QMainWindow): def __init__(self, parent=None): super(MyForm, self).__init__(parent) button1 = QPushButton('Button 1') button2 = QPushButton('Button 1') button1.clicked.connect(lambda: self.on_button(1)) button2.clicked.connect(lambda: self.on_button(2)) layout = QHBoxLayout() layout.addWidget(button1) layout.addWidget(button2) main_frame = QWidget() main_frame.setLayout(layout) self.setCentralWidget(main_frame) def on_button(self, n): print('Button {0} clicked'.format(n))if __name__ == "__main__": import sys app = QApplication(sys.argv) form = MyForm() form.show() app.exec_()
Explains how on_button processes signals from two buttons. We use Lambda to pass button numbers to the slot, or anything else-or even the button component itself (if the slot intends to change the button for transmitting signals to unavailable)
The 2nd method is to use the partial function in functools.
button1.clicked.connect(partial(self.on_button, 1))button2.clicked.connect(partial(self.on_button, 2))
Which method is better? This is a style issue. Personal Opinion, like lambda, clear and flexible.
P143 example of rapid GUI program with Python and QT.
From pyqt4.qtcore import * From pyqt4.qtgui import * From functools import partialimport sysclass bu1 (qwidget): def _ init _ (self, parent = none): Super (bu1, self ). _ init _ (parent) # horizontal box layout = qhboxlayout () # display self. LBL = qlabel ('no button is pressed ') # loop 5 buttons for I in range (5): But = qpushbutton (STR (I) layout. addwidget (but) # signal and slot connection. clicked. connect (self. cliked) # use encapsulation, Lambda but = qpushbutton ('5') layout. addwidget (but). clicked. connect (lambda: Self. on_click ('5') # Use the WHO variable. The result is not normal. False is pressed #. clicked. connect (lambda who = "5": Self. on_click (WHO) # use encapsulation, partial function but = qpushbutton ('6') layout. addwidget (but). clicked. connect (partial (self. on_click, '6') layout. addwidget (self. LBL) # Set the layout self. setlayout (layout) # pass the additional parameter def cliked (Self): bu = self. sender () If isinstance (Bu, qpushbutton): Self. LBL. settext ('% s is pressed' % bu. text () else: Self. LBL. settext ('no effect ') def on_click (self, n): Self. LBL. settext ('% s is pressed' % N) If _ name _ = '_ main _': APP = qapplication (sys. argv) Bu = bu1 () Bu. show () app.exe C _()
Additional Parameters for pyqt signal and slot Transmission