QMessageBox会阻塞Qt中整个主线程的运行吗? [英] Will QMessageBox block the running of the whole main thread in Qt?

查看:1589
本文介绍了QMessageBox会阻塞Qt中整个主线程的运行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Qt的新手

我的情况是:由于某种原因,我必须从主线程发出心跳信号,同时我想使用以下命令创建QMessageBox窗口:

My situation is: For some reason, I have to emit a heartbeat signal from the main thread, at the same time I would like to create a QMessageBox window using:

reply = QMessageBox::question(this, tr("Sure want to quit?"), tr("Sure quit?"), QMessageBox::Yes|QMessageBox::No);

我只希望此消息框阻止其他窗口的用户输入,但我不想阻止心跳信号. 我应该怎么做?还是默认在Qt中完成此操作?

I just want this message box to block user's input from other windows, but I do not want to block the heartbeat signal. How should I do this? Or is this done by default in Qt?

推荐答案

QMessageBox :: question在内部执行事件循环.因此,一切继续运行.您不必为此担心.

QMessageBox::question internally executes the event loop. So everything continues running. You don't need to be worried about this.

但是,使用这些功能可以获得奇怪的效果.例如.如果您的心跳可以打开一个对话框,那么即使已经打开另一个对话框,该对话框也会打开. 还要想象您正在运行一个TCP/IP堆栈.该堆栈可以执行的所有操作将继续发生……无论当前在执行QMessageBox :: question()的任何地方……都像在某个函数中间一样.

However you can get strange effects using such functions. E.g. if your heartbeat could open a dialog that dialog would open too even if another dialog is open already. Also imagine you have a TCP/IP stack running. Everything that this stack can do will continue to happen... whereever QMessageBox::question() is currently executed... like in the middle of some function.

这就是为什么我们在公司中有一条策略,该策略禁止使用QMessageBox :: question()(和类似方法),并且禁止在应用程序中的对话框上调用exec().我们正在堆上创建模式对话框,并改用它们的信号.

This is why we have a policy in our company that forbids to use QMessageBox::question() (and similar) and to call exec() on dialogs in our applications. We are creating modal dialogs on the heap and use their signals instead.

这篇关于QMessageBox会阻塞Qt中整个主线程的运行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆