gpt4 book ai didi

c++ - 当主线程显然未被阻塞时,QProgressDialog卡住

转载 作者:行者123 更新时间:2023-12-02 10:29:07 27 4
gpt4 key购买 nike

我正在开发一个在耗时的计算过程中使用 QProgressDialog 的项目。为了不阻塞主UI线程,将执行计算的类移动到新的 QThread ,并使用通过 Qt::AutoConnection ( QObject::connect 的默认选项)连接的开始和结束信号与主线程同步。我的问题是QProgressDialog有时会冻结,就像在this视频中看到的一样(还附带了gif版本,但它不如视频清晰):
enter image description here
在此动画中,您可以看到启动项目后进度条如何卡在Second check上。应用程序将保持这种状态,直到鼠标再次输入QProgressDialog为止。这只会最终发生,如果将鼠标移到其他窗口上,则更容易发生。而且,它永远不会在同一阶段发生。
该软件的主要部分由StateMachine组成,该代码位于主线程中,并继承QObject并与其余小部件(在本例中为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();
}
驻留在辅助线程中并与 StateMachineWorker交互并执行线程阻塞过程的 StateMachine
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();
}
整个示例可以在 here中找到。如您所见,工作器被移至线程以避免阻塞主UI线程。但是,从上一个视频中可以看出,无论主线程是否被阻止(或者至少我认为它未被阻止),GUI都将变得无响应。
您对我在做什么错有任何想法吗?如何使进度对话框不冻结?如果我将 QProgressDialog设置为 Qt::NonModal而不是 Qt::ApplicationModal,问题就消失了,但是我想阻止其他小部件的输入,因此不能更改它。
这仅在Windows上发生,并且已经在Windows 10,MSVC 19.26.28806.0和Qt版本5.14.2中进行了测试。在具有g++ 7和Qt 5.9.5的Ubuntu中,这不会发生。
PS:我知道 StateMachineStateMacineWorker只是一个对象,它驻留在辅助线程中并通过信号/插槽直接与主线程小部件交互,而不是让 StateMachine驻留在主线程中并与工作人员交互,但是这不是这个问题的主要关注点。

最佳答案

问题显然是Qt中的一些错误。在Windows中使用Qt 5.15.0时,问题消失了。

关于c++ - 当主线程显然未被阻塞时,QProgressDialog卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63017707/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com