The following user says thank you to wysota for this useful post:
Unfortunately, documentation do not specify that 'lives' stands for and no examples is available. I have tried the following code:. This code would most likely work though I have not tested it and I think it does what you want it to do:. MySignal should be sent from thread1 thought I'm not sure on that one, it might be sent from main thread, it doesn't really matter.
No event loop is needed in thread1 since emitting a signal doesn't need an event loop. An event loop is needed in thread2 lanched by exec to receive the signal. This article sums it up: Unfortunately the problem stems from a lack of updates to documentation. Prior to Qt 4. The key to getting slots to execute in a worker thread is to use the moveToThread method as Aiua pointed out. Learn how to make a complex multi-threaded application the easy way and communicate across threads. All passing such as updateCount happens using signals and slots.
When passing data between threads using signals and slots Qt handles.. QT signal to change the GUI out side the main.. Browse other questions tagged windows qt dll or ask your own question. This tutorial comes with examples for Qt's three basic ways of working with threads. Two more examples show how to communicate with a running thread and how a QObject can be placed in another thread, providing service to the main thread.
The following examples can all be compiled and run independently. The source can be found in the examples directory: Creating and destroying threads frequently can be expensive. To avoid the cost of thread creation, a thread pool can be used.
A thread pool is a place where threads can be parked and fetched. We can write the same "hello thread" program as above using the global thread pool. We derive a class from QRunnable.
The code we want to run in another thread needs to be placed in the reimplemented QRunnable:: We want to produce a clock application. The application has a GUI and a worker thread.
The worker thread checks every 10 milliseconds what time it is. If the formatted time has changed, the result will be sent to the GUI thread where it is displayed. Of course, this is an overly complicated way of designing a clock and, actually, a separate thread is unnecessary. We would be better off placing the timer in the main thread because the calculation made in the timer slot is very short-lived.
This example is purely for instructional use and shows how to communicate from a worker thread to a GUI thread. Note that communication in this direction is easy. Communication from the GUI to the worker thread is shown in the next example. Still it is safe to access ClockThread:: This example shows how it is possible to have a QObject in a worker thread that accepts requests from the GUI thread, does polling using a timer and continuously reports results back to the GUI thread.
The actual work including the polling must be implemented in a class derived from QObject. We have called this class WorkerObject in the code shown below. The thread-specific code is hidden in a class called Thread, derived from QThread. Thread has two additional public members. The launchWorker member takes the worker object and moves it to another thread with a started event loop. The call blocks for a very short moment until the thread creation operation is completed, allowing the worker object to be used again on the next line.
The Thread class's code is short but somewhat involved, so we only show how to use the class. The worker thread draws each star onto its own individual image, and it passes each image back to the example's window which resides in the main application thread.
The User Interface We begin by importing the modules we require. We need the math and random modules to help us draw stars. We create a single Worker instance that we can reuse as required.
The user interface consists of a label, spin box and a push button that the user interacts with to configure the number of stars that the thread wil draw. The output from the thread is presented in a QLabel instance, viewer. This will reset the user interface when the thread stops running. We place each of the widgets into a grid layout and set the window's title: Since the start button is the only widget that can cause this slot to be invoked, we simply disable it before starting the thread, avoiding problems with re-entrancy.
Whenever is star is drawn by the worker thread, it will emit a signal that is connected to the addImage slot.