[PyQt] pyqt4 signal/slot using PyObject* and shortcut

Phil Thompson phil at riverbankcomputing.co.uk
Fri Apr 27 16:19:37 BST 2007


On Friday 27 April 2007 3:45 pm, Andreas Pakulat wrote:
> On 27.04.07 11:30:04, Pradnyesh Sawant wrote:
> >    def run(self):
> >        cntr = 0
> >        while cntr < 10:
> >            cntr += 1
> >            self.emit(QtCore.SIGNAL("showCntr1(PyObject*)"), (cntr,
> > "a"))   # line 1
> >            self.emit(QtCore.SIGNAL("showCntr2"), (cntr, "a"))
> >          # line 2
> >            time.sleep(0.2)
>
> ...
>
> > if __name__ == "__main__":
> >    app = QtGui.QApplication(sys.argv)
> >    dialog = Gui()
> >    cntr = Counter()
> >    cntr.start()
> >    QtCore.QObject.connect(cntr, QtCore.SIGNAL("showCntr1(PyObject*)"),
> >            dialog.showCntr1, QtCore.Qt.QueuedConnection)
> >    QtCore.QObject.connect(cntr, QtCore.SIGNAL("showCntr2"),
> >            dialog.showCntr1, QtCore.Qt.QueuedConnection)
> >    sys.exit(dialog.exec_())
> > #################################################################
> > If i comment out "line 1", then i get the following output:
>
> You don't keep a reference to the object that you send via the signal.
> You need to do that, as a queued connection behaves different from a
> direct connection (which is a direct function call). Thats why you see
> all kinds of weird results.

Sorry, but that's wrong. PyQt handles the reference counts of arguments to 
shortcircuit signals automatically.

> I couldn't make the line 1 work though, it always tells me that argument
> 0 of the signal has an invalid type.

Because you need to use PyQt_PyObject instead of PyObject * (which never 
worked properly).

All of the original poster's problems are down to using an old version of 
PyQt4.

Phil


More information about the PyQt mailing list