Working with QRunnable processes
In this recipe, we will learn how to use another type of high-level method to easily create a multithreading Qt 6 application. We will use the QRunnable
and QThreadPool
classes in this recipe.
How to do it…
- Create a new Qt widget application project and then a new C++ class called
MyProcess
, which inherits theQRunnable
class. - Next, open up
myprocess.h
and add the following headers:#include <QRunnable> #include <QDebug>
- Then, declare the
run()
function, as follows:class MyProcess : public QRunnable { public: MyProcess(); void run(); };
- After that, open up
myprocess.cpp
and define therun()
function:void MyProcess::run() { int myNumber = 0; for (int i = 0; i < 100000000; ++i) { ...