QDialog::exec() 阻止应用程序

QDialog::exec() blocks the application

本文关键字:应用程序 exec QDialog      更新时间:2023-10-16

我目前有一个formA,它使用从QDialog继承的另一个表单请求用户输入。使用QDialog::exec提示表单。现在的问题是,将有多个表单A实例,因此每当表单A中的任何一个打开另一个表单作为对话框时,整个应用程序都会阻止。目前我有这样的东西

if(formUserInputRequired->exec()==1) //Block until the user selects from a form
{
}

有没有办法使QDialog::exec不阻止整个应用程序,我只是希望它只阻止调用它的表单的实例或类似的东西,但绝对不是整个应用程序?

更新:我不需要阻塞窗口。但是,我想找到一种方法来知道用户何时完成另一种形式的输入,以便原始表单可以处理该数据

在对话框中调用 setWindowModality 方法,Qt::WindowModal 作为参数。

Qt::NonModal          0  The window is not modal and does not block input to other windows.
Qt::WindowModal       1  The window is modal to a single window hierarchy and blocks input to its parent window, all grandparent windows, and all siblings of its parent and grandparent windows.
Qt::ApplicationModal  2  The window is modal to the application and blocks input to all windows.

你可以改用 show(),然后为了获取对话框结果,你将接受信号连接到 formA 的插槽来处理它,就像:

connect(formUserInputRequired, SIGNAL(accept()), this, SLOT(acceptClicked());
formUserInputRequired->show();

将对话框的模态设置为Qt::WindowModal(QDialog 的默认值为 Qt::ApplicationModal

您可以使用

show()方法代替exec(),因为exec方法都有自己的事件循环。