Will QMessageBox block the running of the whole main thread in Qt?

混江龙づ霸主 提交于 2019-12-08 10:30:30

问题


I am new to Qt

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);

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?


回答1:


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

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.

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.



来源:https://stackoverflow.com/questions/26502673/will-qmessagebox-block-the-running-of-the-whole-main-thread-in-qt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!