gpt4 book ai didi

c++ - 如何在 QT 中制作模态 QProgressDialog?

转载 作者:行者123 更新时间:2023-11-28 01:27:55 31 4
gpt4 key购买 nike

我正在从线程调用 QProgressDialog 并且无法将其设置为模态窗口,即使我将 setModal 设置为 true。我希望在 QProgressDialog 运行时阻塞主窗口。

以下是我的一段代码。

通用函数.h
QProgressDialog *progressBarDialog;

通用函数.cpp

     void GenericFunc::testSlot()
{
int numTasks = 4500;
progressBarDialog = new QProgressDialog("Task in progress.", "Cancel", 0, numTasks);
progressBarDialog->setWindowModality(Qt::WindowModal);
progressBarDialog->setModal(true);
progressBarDialog->exec();
}

最佳答案

QProgressDialog 类是一个 GUI 类。您不能在工作线程中实例化它。

http://doc.qt.io/qt-5/thread-basics.html#gui-thread-and-worker-thread

GUI Thread and Worker Thread

As mentioned, each program has one thread when it is started. This thread is called the "main thread" (also known as the "GUI thread" in Qt applications). The Qt GUI must run in this thread. All widgets and several related classes, for example QPixmap, don't work in secondary threads. A secondary thread is commonly referred to as a "worker thread" because it is used to offload processing work from the main thread.

使用信号槽将进度数据从工作线程传递到 gui 线程。

另一个问题是您将其设置为 WindowModal,但此 progressBarDialog 没有任何父窗口,因此它不会阻塞任何父窗口链。

http://doc.qt.io/qt-5/qt.html#WindowModality-enum

关于c++ - 如何在 QT 中制作模态 QProgressDialog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52990508/

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