There are several options.
1. Subclass QRunnable
Subclass QRunnable and use QThreadPool to run it in a separate thread. Use signals to communicate with the user interface. An example of this:
class LongTask: public QRunnable { void run() {
Please note that you do not need to worry about controlling the flow or lifetime of your QRunnable. For communication, you can connect your custom signals before starting QRunnable.
This is a different approach and may not suit your problem. Basically, the way it works is this: you get a handle to the future return value of a long task. When you try to restore the return value , it will either immediately send it to you, or wait for the task to complete if it has not already been completed. Example:
QFuture<int> future = QtConcurrent::run(longProcessing, param1, param2);
3. Subclass QThread
You probably do not need this, but it is also not difficult. This is very similar to # 1, but you need to control the flow yourself. Example:
class MyThread : public QThread { public: void run() {
Similar to QRunnable, you can use signals to communicate with the user interface.
source share