Will QMessageBox block the running of the whole ma

2019-06-10 03:54发布

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条回答
Rolldiameter
2楼-- · 2019-06-10 04:23

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.

查看更多
登录 后发表回答