0

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?

Nyaruko
  • 4,329
  • 9
  • 54
  • 105

1 Answers1

4

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.

Silicomancer
  • 8,604
  • 10
  • 63
  • 130
  • In my case ,will "to call exec() on dialogs" stop the heartbeat signal? Could you give a short example of how it is done in your company, really thanks. – Nyaruko Oct 22 '14 at 08:10
  • 2
    @Nyaruko No, `QDialog::exec` does the same thing. It creates a local event loop. And local event loops can be bad. You can read [this article](https://blogs.kde.org/2009/03/26/how-crash-almost-every-qtkde-application-and-how-fix-it-0) to find out why. – thuga Oct 22 '14 at 08:18
  • Most notably what is not done in a local event loop is `deleteLater` execution. Exactly to prevent the `this` got `delete`d crash. (won't help you if you actually `delete` the parent but if you used `deleteLater` it will. – ratchet freak Oct 22 '14 at 09:09
  • @nyaruko: How can a replacement for QMessageBox::question etc. be implemented that will not execute the main event loop. Can you link a example? – RED SOFT ADAIR May 25 '22 at 11:08