I`m starting with PyQt4 and right now I have a problem with events.
I have one main class let say MainWindow. MainWindow has a list of buttons of type ButtonX (inherence form QPushButton). I would like to achieve one of 2 solutions (depends which is easier).
1) After click one of the button from the list I would like to run a one method of MainWindow. I would like to be able to read a source of event there (recognize clicked button)
2) Second solution is to run a method defined in ButtonX class.
What I tried is:
QtCore.QObject.connect(self.getButton(0, 0), QtCore.SIGNAL("clicked()"), self.getButton(0, 0).buttonMethod())
QtCore.QObject.connect(self.getButton(0, 0), QtCore.SIGNAL("clicked()"), self.getButton(0, 0), QtCore.SLOT("incrementValue()"))
and even this line occure suspend Python interpreter
QtCore.QObject.connect(self.getButton(0, 0), QtCore.SIGNAL("clicked()"), self.getButton(0, 0), QtCore.SLOT("incrementValue"))
You can access the source of an event using QObject.sender(). But, as the instructions indicate, it’s often better to use QSignalMapper or do things in a more object oriented fashion.
Look carefully at what you typed for the last argument of the first connect call:
The above will evaluate
getButtonwith parameters0, 0and then, on that object, call thebuttonMethodmethod. So, unlessbuttonMethodreturns a method, you are using the return value ofbuttonMethodas the last parameter to the connect call. If this method returns a function, then this is just fine.Rather, I would expect to see something like the following:
Take a look at the PyQt examples directory provided with PyQt as they’ll demonstrate the exact syntax and serve as good examples.
Here’s one small example: