Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to react to 'Close window' in right-click menu of task bar in Windows

Tags:

windows

qt

qt5

Using Qt 5.3.0 and Windows 8.1:

Don't know if I'm missing something or if this is an oversight in Qt, but when I simply have a QMainWindow that opens a QDialog (with exec()) and then try to use the 'Close window' function of the right-click menu of the Windows task bar icon (while the dialog is still open), the application is not closing as expected, but nothing happens. I also do not get a closeEvent in the QDialog or the QMainWindow.

When only the QMainWindow is open, the application is closed successfully and I'm also getting a closeEvent.

This is actually also reproducible when e.g. using Qt Designer and opening an additional dialog and then trying to use the 'Close window' function.

Any ideas how to fix this behavior?

like image 657
Robert Avatar asked May 13 '15 14:05

Robert


2 Answers

The behavior you're seeing is not really surprising. In fact, it's exactly what happens with a (mostly) well-behaved Windows app like Notepad, so I'm not sure I'd even call it a bug.

Open Notepad, and select Help->About to get a modal dialog. Now choose Close from the task bar icon. Nothing happens.

The Close from the task bar is sent to the main window as if the user had selected the Close option from the "system" menu. That arrives as a WM_SYSCOMMAND with SC_CLOSE. If you don't handle that explicitly, then DefWindowProc turns it into a WM_CLOSE message, which most main windows handle.

But if you have a modal dialog open, the main window is disabled and thus doesn't get the message.

One way to fix it would be not to have modal dialogs and instead simulate modality with modeless dialogs. That would allow the main window to receive and respond to the message. But that would be a lot of work for a small fix.

like image 117
Adrian McCarthy Avatar answered Sep 20 '22 05:09

Adrian McCarthy


If you call your QDialog via QDialog::show() passing the MainWindow as parent ( QDialog *dialog = new QDialog(this) ) where this is the pointer to your MainWindow, it will work. However the dialog won't be modal anymore. I don't know if modality is important in your case.

like image 37
Varius Avatar answered Sep 18 '22 05:09

Varius