Passing Argument To Pyqt Signal Connection
Solution 1:
Here is a different approach: instead of attaching the data as an argument to the signal handler, attach it to the menu-item itself. This offers much greater flexibility, because the data is not hidden inside an anonymous function, and so can be accessed by any part of the application.
It is very easy to implement, because Qt already provides the necessary APIs. Here is what your example code would look like if you took this approach:
for j in range(0, len(self.InputList)):
arrow = QtGui.QPushButton(self)
arrow.setGeometry(QtCore.QRect(350, 40*(j+3)+15, 19, 23))
menu = QtGui.QMenu(self)
group = QtGui.QActionGroup(menu)
for element in SomeList:
action = menu.addAction(element)
action.setCheckable(True)
action.setActionGroup(group)
action.setData(j)
arrow.setMenu(menu)
group.triggered.connect(self.SomeFunction)
def SomeFunction(self, action):
print(action.data())
Solution 2:
Be advised that you are using a deprecated version of SIGNAL/SLOT implementation in PyQt that has been removed in PyQt5 (and even its current implementation in PyQt4 is somewhat buggy).
If possible, I would change the SIGNAL/SLOT syntax in your code.
The way the new Signal/Slot mechanism works is this:
classWorker(QThread):
stateChanged = Signal(int)
....
defsome_method(self):
self.stateChanged.emit(1)
In the GUI thread, you would similarly have something like this (assuming worker = Worker()
is defined somewhere:
classGUI(QDialog)
worker = Worker()
def__init__(self, parent=None):
super(GUI, self).__init__(parent)
self.worker.stateChanged.connect(self.my_desired_method)
defmy_desired_method(self, param):
print param #prints out '1'
Of course, this code wouldn't work "out of the box", but it's a general concept of how Signals/Slots should be handled in PyQt (and PySide).
Solution 3:
I think this should help.
self.op = "point"
QtCore.QObject.connect(self.radioButton, QtCore.SIGNAL("clicked(bool)"),lambda : self.anyButton(self.radioButton.isChecked(),self.op))
defanyButton(self,kol,vv):
print kol
print vv
The output is
>>> True
point
you can take a look at this : PyQt sending parameter to slot when connecting to a signal
Post a Comment for "Passing Argument To Pyqt Signal Connection"