问题
This example works: (Here my function has no input parameters)
QtCore.QObject.connect(ComboJobType1_1, QtCore.SIGNAL(_fromUtf8("currentIndexChanged(QString)")) ,self.InputChanged())
But, It is necessary for me to send a parameter so I try this: (After updating my function to expect one parameter):
QtCore.QObject.connect(ComboJobType1_1, QtCore.SIGNAL(_fromUtf8("currentIndexChanged(QString)")) ,self.InputChanged("ComboJobType1_1"))
Please note that the first example is working, the only problem (I guess) is sending a parameter to my function (I must know the name of the comboBox when I'm in the function).
Please advice... Thank you! Wish you a great day, Dolphin
回答1:
ref https://eli.thegreenplace.net/2011/04/25/passing-extra-arguments-to-pyqt-slot
button1.clicked.connect(lambda: self.on_button(1))
button2.clicked.connect(lambda: self.on_button(2))
def on_button(self, n):
print('Button {0} clicked'.format(n))
worked for me, tht gives it a simple lambda (turn it to funciton ref instead of execute ?
回答2:
You need to provide the emitting object ("from_object") and the slot handler ("self.my_handler"):
QtCore.QObject.connect(from_object, QtCore.SIGNAL("nameOfSignal(QString)"), self.my_handler)
回答3:
Another way you can import partial function to pass arguments to slot function.
from functools import partial
QtCore.QObject.connect(ComboJobType1_1, QtCore.SIGNAL(_fromUtf8("currentIndexChanged(QString)")) ,partial(self.InputChanged, "ComboJobType1_1"))
来源:https://stackoverflow.com/questions/11223599/python-pyqt-send-parameters-to-the-connected-signal-function