Per-Thread Event Loop
This is the approach used for implementing the Mandelbrot and the Blocking Fortune Client example. It emits signals to indicate that the thread started or finished executing, and provides a few slots as well.
You must ensure that all objects created in a thread are deleted before you delete the QThread. The child of a QObject must always be created in the thread where the parent was created.
Documentation contributions included herein are the copyrights of their respective owners. The initial thread starts its event loops using QCoreApplication:: The connection type can be specified by passing an additional argument to connect. If you are calling a function on an QObject subclass that doesn't live in the current thread and the object might receive events, you must protect all access to your QObject subclass's internal data with a mutex; otherwise, you may experience crashes or other undesired behavior.
The thread in which a QObject lives is available using QObject:: Per-Thread Event Loop Each thread can have its own event loop. Calling delete on a QObject from a thread other than the one that owns the object or accessing the object in other ways is unsafe, unless you guarantee that the object isn't processing events at that moment.
Qt and respective logos are trademarks of The Qt Company Ltd. The slot is executed in the emitter's thread, which is not necessarily the receiver's thread. This means static instances of QObject are also not supported. For example, you cannot start a timer or connect a socket in a thread that is not the object's thread.
This can be done easily by creating the objects on the stack in your run implementation. The slot is executed in the receiver's thread. A properly structured single or multi-threaded application should make the QApplication be the first created, and last destroyed QObject. The thread emits a signal when it is done rendering the fractal.
On the other hand, you can safely emit signals from your QThread:: Unique Connection The behavior is the same as the Auto Connection, but the connection is made only if it does not duplicate an existing connection. As noted earlier, QCoreApplication:: The child of a QObject must always be created in the thread where the parent was created.
Using this type to connect objects in the same thread will cause deadlock. Calling deleteLater won't work either. This is explained in more detail in the Signals and Slots Across Threads section below. Blocking Queued Connection The slot is invoked as for the Queued Connection, except the current thread blocks until the slot returns.
In practice, the impossibility of using GUI classes in other threads than the main thread can easily be worked around by putting time-consuming operations in a separate worker thread and displaying the results on screen in the main thread when the qt slot thread thread is finished.
Blocking Queued Connection The slot is invoked as for the Queued Connection, except the current thread qt slot thread until the slot returns. Auto Connection default If the signal is emitted in the thread which the receiving object has affinity then the behavior is the same as the Direct Connection.
Threads and QObjects | Qt