[PyQt] Connected signals and moveToThread

mir amicitas amicitas at gmail.com
Wed Feb 23 17:37:17 GMT 2011


Looks like I solved this on my own.   For this to work properly I need
to make sure that the slot that I am calling has a @QtCore.pyqtSlot()
decorator.  Once this is done, the order of when I call connect() and
moveToThread() no longer matters.  It would be useful if a description
of this behavior could be added to the Reference Guide.

With the slot decorator this works with both the new-style and
old-style signal and slot styles, however if the old-style is used,
all four parameters must be given when connected.


Novimir


On Wed, Feb 23, 2011 at 3:25 AM, mir amicitas <amicitas at gmail.com> wrote:
> 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