How it works...

QThread is an alternative method for running a process asynchronously, besides using the QFuture class. It gives us more control compared with QFuture, which we will demonstrate in the following recipe.

Do note that the QObject class that gets moved to the working thread cannot have any parent, as Qt is designed in such a way that an entire object tree must exist in the same thread. Therefore, all of the children of a QObject class will also be moved to the working thread when you call moveToThread().

Use a signals and slots mechanism if you want your working thread to communicate with the main thread.

We used the started signal that's provided by the QThread class to inform our working object to start the calculation since the working thread has already been created.

Then, when the calculation has been completed, we emit the showResult and doneProcess signals to inform the thread to quit, while passing the final result to the main thread for it to be printed.

Lastly, we also use the signals and slots mechanism to safely delete both the working thread and working object when everything is done.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset