如何在 QT 中制作模态 QProgressDialog?
how to make a modal QProgressDialog in QT?
我正在从线程调用 QProgressDialog,但无法将其设置为模态 window,即使我将 setModal 设置为 true。我希望 mainwindow 在 QProgressDialog 运行时被阻塞。
以下是我的一段代码。
GenericFunc.h
QProgressDialog *progressBarDialog;
GenericFunc.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 class 是一个 GUI class。您不能在工作线程中实例化它。
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
没有任何父级,因此它不会阻止任何父级 window(s) 链。
我正在从线程调用 QProgressDialog,但无法将其设置为模态 window,即使我将 setModal 设置为 true。我希望 mainwindow 在 QProgressDialog 运行时被阻塞。
以下是我的一段代码。
GenericFunc.h
QProgressDialog *progressBarDialog;
GenericFunc.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 class 是一个 GUI class。您不能在工作线程中实例化它。
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
没有任何父级,因此它不会阻止任何父级 window(s) 链。