How to do it…

Let's get started by preforming the following steps:

  1. Create a new Qt Widgets Application project. Then, go to File | New File or Project... and create a C++ Class:

  1. After that, name the new class MyWorker and make it inherit from the QObject class. Don't forget to include the QObject class by default as well:

  1. Once you have created the MyWorker class, open up myworker.h and add the following headers at the top:
#include <QObject>
#include <QDebug>
  1. After that, add the following signals and slot functions to the file as well:
signals:
void showResults(int res);
void doneProcess();

public slots:
void process();
  1. Next, open up myworker.cpp and implement the process() function:
void MyWorker::process() {
int result = 0;
for (int i = 0; i < 2000000000; ++i) {
result += 1;
}
emit showResults(result);
emit doneProcess();
}
  1. After that, open up mainwindow.h and add the following headers at the top:
#include <QDebug>
#include <QThread>
#include "myworker.h"
  1. Then, declare a slot function, as shown in the following code:
public slots:
void handleResults(int res);
  1. Once you're done, open up mainwindow.cpp and implement the handResults() function:
void MainWindow::handleResults(int res) {
qDebug() << "Handle results" << res;
}
  1. Lastly, we will add the following code to the class constructor of the MainWindow class:
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){
ui->setupUi(this);
QThread* workerThread = new QThread;
MyWorker *workerObject = new MyWorker;
workerObject->moveToThread(workerThread);
connect(workerThread, &QThread::started, workerObject, &MyWorker::process);
connect(workerObject, &MyWorker::doneProcess, workerThread, &QThread::quit);
connect(workerObject, &MyWorker::doneProcess, workerObject, &MyWorker::deleteLater);
connect(workerObject, &MyWorker::showResults, this, &MainWindow::handleResults);
connect(workerThread, &QThread::finished, workerObject, &MyWorker::deleteLater);
workerThread->start();
}
  1. Build and run the program now. You should see that the main window pops out and does nothing for a couple of seconds before a line of message is printed on the debug window:
Final result: 2000000000
  1. The result was calculated in a separate thread, which is why the main window can display smoothly, and can even be moved around by a mouse during the calculation. To see the difference when running the calculation on the main thread, let's comment out some of the code and call the process() function directly:
//QThread* workerThread = new QThread;
MyWorker *workerObject = new MyWorker;
//workerObject->moveToThread(workerThread);
//connect(workerThread, &QThread::started, workerObject, &MyWorker::process);
//connect(workerObject, &MyWorker::doneProcess, workerThread, &QThread::quit);
connect(workerObject, &MyWorker::doneProcess, workerObject, &MyWorker::deleteLater);
connect(workerObject, &MyWorker::showResults, this, &MainWindow::handleResults);
//connect(workerThread, &QThread::finished, workerObject, &MyWorker::deleteLater);
//workerThread->start();
workerObject->process();
  1. Build and run the project now. This time, the main window will only appear on the screen once the calculation has been done. This is because the calculation was blocking the main thread (or GUI thread) and prevented the main window from being displayed.
..................Content has been hidden....................

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