I am working on a project that uses a QProgressDialog during a time-consuming computation process. In order to not block the main UI thread, the class that does the computation is moved to a new QThread and it is synchronized with the main thread using a start and finish signal connected using Qt::AutoConnection, the default option for QObject::connect. My problem is that the QProgressDialog sometimes freezes as can be seen in the this video (also attached a gif version, but it is not as clear as the video):
In this animation, you can see how after starting the project, the progress bar gets stuck at Second check. And the application stays like this until the mouse enters again the QProgressDialog. This only happens eventually, and it is more prone to happen if you move the mouse over other windows. Moreover it never happens at the same stage.
The main part of the software consist of a StateMachine that lives in the main thread and inherits QObject and interacts with the rest of widgets (in this case the QProgressDialog):
state_machine.h
#pragma once
#include <QObject>
class StateMachine : public QObject
{
Q_OBJECT
public:
static StateMachine* open(QThread* worker_thread, QWidget* parent);
signals:
void finished();
void datasetOpened();
void requestCloseDataset();
void errorMessage(QString text);
void progressShow();
void progressHide();
void progressText(QString text);
void requestProcess1();
void requestProcess2();
void requestProcess3();
void requestProcess4();
protected slots:
void process1Done();
void process2Done();
void process3Done();
void process4Done();
protected:
StateMachine(QThread* worker_thread, QWidget* parent = nullptr);
};
state_machine.cpp
#include "state_machine.h"
#include <QWidget>
#include "progress_dialog.h"
#include "state_machine_worker.h"
StateMachine *StateMachine::open(QThread *worker_thread, QWidget *parent)
{
return new StateMachine(worker_thread, parent);
}
StateMachine::StateMachine(QThread *worker_thread, QWidget *parent) : QObject(parent)
{
// Connect
connect(this, &StateMachine::finished, this, &QObject::deleteLater);
// Create worker
StateMachineWorker *worker = new StateMachineWorker();
worker->moveToThread(worker_thread);
connect(this, &StateMachine::finished, worker, &QObject::deleteLater);
/// Dataset file license
connect(this, &StateMachine::requestProcess1, worker, &StateMachineWorker::process1);
connect(worker, &StateMachineWorker::process1Done, this, &StateMachine::process1Done);
/// Sonar list
connect(this, &StateMachine::requestProcess2, worker, &StateMachineWorker::process2);
connect(worker, &StateMachineWorker::process2Done, this, &StateMachine::process2Done);
/// Sonar limit
connect(this, &StateMachine::requestProcess3, worker, &StateMachineWorker::process3);
connect(worker, &StateMachineWorker::process3Done, this, &StateMachine::process3Done);
/// Dataset initialize
connect(this, &StateMachine::requestProcess4, worker, &StateMachineWorker::process4);
connect(worker, &StateMachineWorker::process4Done, this, &StateMachine::process4Done);
// Create progress
ProgressDialog *progress = ProgressDialog::create("OpenProject", "", "", 0, 0, parent, Qt::ApplicationModal);
connect(this, &StateMachine::progressHide, progress, &QWidget::hide);
connect(this, &StateMachine::progressShow, progress, &QWidget::show);
connect(this, &StateMachine::finished, progress, &QWidget::hide);
connect(this, &StateMachine::progressText, progress, &ProgressDialog::setLabelText);
connect(this, &StateMachine::finished, progress, &QObject::deleteLater);
// Check dataset file license
emit progressText("First check");
emit progressShow();
emit requestProcess1();
}
void StateMachine::process1Done()
{
emit progressText("Second check");
emit requestProcess2();
}
void StateMachine::process2Done()
{
emit progressText("Third check");
emit requestProcess3();
}
void StateMachine::process3Done()
{
emit progressText("Fourth check");
emit requestProcess4();
}
void StateMachine::process4Done()
{
emit progressHide();
emit requestCloseDataset();
emit datasetOpened();
emit finished();
}
A StateMachineWorker that lives in a secondary thread and interacts with the StateMachine and does the process that are thread blocking.
state_machine_worker.h
#pragma once
#include <QObject>
class StateMachineWorker : public QObject
{
Q_OBJECT
public:
StateMachineWorker(QObject* parent = nullptr);
signals:
void process1Done();
void process2Done();
void process3Done();
void process4Done();
public slots:
void process1();
void process2();
void process3();
void process4();
};
state_machine_worker.cpp
#include "state_machine_worker.h"
#include <thread>
StateMachineWorker::StateMachineWorker(QObject *parent) : QObject(parent)
{
}
void StateMachineWorker::process1()
{
std::this_thread::sleep_for(std::chrono::seconds(2));
emit process1Done();
}
void StateMachineWorker::process2()
{
std::this_thread::sleep_for(std::chrono::seconds(2));
emit process2Done();
}
void StateMachineWorker::process3()
{
std::this_thread::sleep_for(std::chrono::seconds(2));
emit process3Done();
}
void StateMachineWorker::process4()
{
std::this_thread::sleep_for(std::chrono::seconds(2));
emit process4Done();
}
The entire example can be found here. As you can see, the worker is moved to the thread to avoid blocking the main UI thread. However, as it can be seen in the previous video, the GUI becomes unresponsive regardless of the main thread non being blocked (or at least I think it is not blocked).
Do you have any idea on what am I doing wrong? How can I make the progress dialog not freeze? If I set the QProgressDialog to Qt::NonModal instead of Qt::ApplicationModal the problem disappears, but I want to block the input to the other widgets, so changing that is not an option.
This only happens on Windows and has been tested with Windows 10, MSVC 19.26.28806.0 and Qt Version 5.14.2. In Ubuntu with g++7 and Qt 5.9.5 it does not happen.
PS: I know that StateMachine and the StateMacineWorker as it coulb be just an object that lives in the secondary thread and interacts directly with the main thread widgets through signals/slots rather than having StateMachine live in the main thread and interact with the worker, but this is not the main concern of this question.
