Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt 5.1 - QMessageBox Bug? Program Exits(0) if QMessageBox is called while QDialog is hidden

Tags:

c++

qt

I seem to have discovered an annoying issue with Qt 5.1.

Let's say for example you have a system tray icon (QSystemTrayIcon) and you hide your form (QDialog), so:

this->hide();

Then, while the form is hidden, your app displays a message box:

QMessageBox::information(0, "Test", "Test");

Once the user hits Ok to close the dialog, the program exits with exit code 0. So, it doesn't crash, but it politely exits.

The only work around that I know off is to use the WIN32 API on Windows and the MessageBox function. This is not what I want to do.

Is this a bug?

like image 960
rwx Avatar asked Jan 11 '23 18:01

rwx


1 Answers

By default, a Qt application closes when the last window is closed (in your case, when you close the QMessageBox).

You can add this code to keep your application running:

qApp()->setQuitOnLastWindowClosed(false);
like image 76
Dimitry Ernot Avatar answered Feb 05 '23 08:02

Dimitry Ernot