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

Giuseppe Corbelli giuseppe.corbelli at copanitalia.com
Mon Apr 28 09:34:58 BST 2014


On 28/04/2014 10:01, Phil Thompson wrote:
> On 28/04/2014 8:26 am, 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.
>
> Does a C++ version do the same?

Was just trying it out. Seems the C++ version is OK, see below example

-------- test.h -------------

#include <QCoreApplication>

class CApp : public QCoreApplication
{
     Q_OBJECT

public:
     CApp(int argc, char** argv) : QCoreApplication(argc, argv) {};

public slots:
     void emit_signal();
     void myslot();

signals:
     void mysignal();
};

----------- test.cpp ---------------

#include <cassert>
#include <QtGlobal>
#include <QTimer>
#include "test.h"


void CApp::emit_signal()
{
     for (int i=0; i < 1000; i++)
         QObject::connect(this, SIGNAL(mysignal()), this, SLOT(myslot()), 
Qt::AutoConnection|Qt::UniqueConnection);
     assert(this->receivers(SIGNAL(mysignal())) == 1);
     emit mysignal();
}

void CApp::myslot()
{
     qDebug("MySlot running");
}

int main(int argc, char* argv[])
{
     CApp app(argc, argv);
     QTimer t;
     QObject::connect(&t, SIGNAL(timeout()), &app, SLOT(emit_signal()));
     t.start(500);
     return 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