[PyQt] Signal/slot bug when doing multiple connects and Qt::UniqueConnection

Giuseppe Corbelli giuseppe.corbelli at copanitalia.com
Mon Apr 28 10:39:46 BST 2014


On 28/04/2014 09:26, Giuseppe Corbelli wrote:
> Hi all
> I've run into an "interesting" side effect, admittedly misusing the
> signal-slot mechanism. Basically I'm doing multiple connections using the
> Qt::UniqueConnection parameter. As expected the slot is run always a single
> time, but there's something weird going on in the background. The receivers()
> method returns increasing numbers and this little example will kill the CPU in
> a few iteractions.

Update:
multiple calls to:

self.mysignal.connect(self.myslot, QtCore.Qt.UniqueConnection)

run without errors if the slot is NOT decorated with pyqtSlot.
This is the case of the example just posted.
If the slot is a @pyqtSlot() the second call fails with a TypeError exception 
but the number of QObject::receivers() stays 1 and the program performs as 
expected. See below an example that performs correctly (or at least it seems 
so) just using the decorator.


#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import logging
import datetime

from PyQt4 import QtCore

logging.basicConfig(level=logging.DEBUG)

logger = logging.getLogger('blah')

class CApp(QtCore.QCoreApplication):
     mysignal = QtCore.pyqtSignal()

     def emit_signal(self):
         try:
             self.mysignal.connect(self.myslot, QtCore.Qt.UniqueConnection)
         except TypeError:
             pass
         assert(self.receivers(self.mysignal.signal) == 1)
         self.mysignal.emit()

     @QtCore.pyqtSlot()
     def myslot(self):
         pass


app = CApp(sys.argv)
t = QtCore.QTimer()
t.timeout.connect(app.emit_signal)
t.start(1)

sys.exit(app.exec_())

-- 
             Giuseppe Corbelli
WASP Software Engineer, Copan Italia S.p.A
Phone: +390303666318  Fax: +390302659932
E-mail: giuseppe.corbelli at copanitalia.com


More information about the PyQt mailing list