Using threads
Qt 6 provides multiple methods to create and use threads. You can choose between high-level methods and low-level ones. High-level methods are much easier to get started but are limited in what you can do with them. Conversely, low-level methods are more flexible but not beginner-friendly. In this recipe, we will learn how to use one of the high-level methods to easily create a multithreading Qt 6 application.
How to do it…
Let’s learn how to create multithreading applications by following these steps:
- Create a Qt widget application and open up
main.cpp
. Then, add the following headers at the top of the file:#include <QFuture> #include <QtConcurrent/QtConcurrent> #include <QFutureWatcher> #include <QThread> #include <QDebug>
- Then, create a function called
printText()
before themain()
function:void printText(QString text, int count) { for (int i = 0; i < count; ++i) ...