[PyQt] Connected signals and moveToThread

mir amicitas amicitas at gmail.com
Wed Feb 23 08:25:30 GMT 2011


I am a little bit confused about how connected signals behave when
objects are moved to a new thread.  I was hoping someone here might be
able to enlighten me a bit.

I am trying to create a simple worker thread using the following code:

        self.listener = Listener()
        self.test_thread = TestThread()

        self.listener.moveToThread(self.test_thread)
        self.test_thread.started.connect(self.listener.listen)

        self.test_thread.start()


Where TestThread() is simply a QThread with self.exec() added to the
run() method.  The above code works great and my worker method is run
in the test_thread.  However, if I change the order of the
moveToThread and the connect statement things no longer work.

        self.listener = Listener()
        self.test_thread = TestThread()

        self.test_thread.started.connect(self.listener.listen)
        self.listener.moveToThread(self.test_thread)

        self.test_thread.start()


Now the worker method is run in the original thread, not the one that
I want. In addition to execution in the main thread being blocked, I
used a bunch of print(QtCore.QThread.currentThread()) statements to
determine which thread things were running in.


My understanding was that this should have worked in both cases (see
for example: http://labs.qt.nokia.com/2006/12/04/threading-without-the-headache/).

Could anyone explain to me how I am misunderstanding this?


Novimir Pablant


More information about the PyQt mailing list