Signal Slot Qthread

28 Aug 2011 Matteo Mattei pythonpysideqtthreadSlot

For signals, the thread affinity doesn’t matter. But for slots it matters a lot. Subclassing QThread. Now you can perhaps appreciate why subclassing QThread can be a problem. If you have anything using slots in the subclass objects, the thread affinity will mean that it will use the original thread. Cross-thread signal-slot connections are implemented by dispatching a QMetaCallEvent to the target object. A QObject instance can be moved to a thread, where it will process its events, such as timer events or slot/method calls. To do work on a thread, first create your own worker class that derives from QObject. Then move it to the thread.

The signal timeout is emitted from sub-thread As timer and worker live in the same thread, their connection type is direct connection. The slot get called in the same thead in which signal get emitted. While in this example. QML Beginners: Core Beginners: https://www.udemy.com/course/qt-core-for-b.

In these days I started studying PySide. After some days spent in reading lot of stuff, I thought that a real example could be useful for who intends to start learning PySide as well. In this example I can show you how you can implement a custom signal (MySignal) together with the usage of threads with QThread.

Qthread

The following code creates a window with two buttons: the first starts and stop a thread (MyThread) that runs a batch that prints a point in the stdout every seconds continuously. The second button lets you only start another thread (MyLongThread) that prints an asterisk in the stdout every second for 10 seconds.

This example uses the api version 2 (introduced with PyQt 4.5) to connect signals to slots.

For more information you can look at:

  • QThread documentation: http://doc.qt.nokia.com/latest/qthread.html
  • PySide signals and slots: http://developer.qt.nokia.com/wiki/Signals_and_Slots_in_PySide
  • PyQt api 2 on PySide: http://www.pyside.org/docs/pseps/psep-0101.html
Qthread signal slot examplePlease enable JavaScript to view the comments powered by Disqus.comments powered by Disqus

Qthread Signal Slot

Signal Slot Qthread

Qthread Signal Slot Example

Related Posts