Search code examples
c++qtmodal-dialogmessagebox

Modal QMessageBox does not behave like native Windows dialogs


My application has a dialog that asks the user via a QMessageBox whether he wants to discard all changes he made or wants to keep editing. I want this dialog to be modal to the whole application.

I read somewhere that this is the standard behavior for a QMessageBox, so I dont have to set it explicitly with something like:

mbox.setWindowModality(Qt::ApplicationModal);

I wonder why it behaves differently from other modal dialogs in the OS (Windows 7 in my case). On the one hand it functions like it should, i.e. all other input methods in the application are blocked until the user answeres the dialog. However, it doesn't 'blink'* if the user clicks any other window of the application. Is there any way to get Qt to behave like a native Windows dialog?

Thanks in advance!


*If you don't know what I mean with this 'blinking': Just open notepad on a Windows OS, type some text and try to close it. A dialog pops up that asks to save, discard or keep editing. Now click somewhere on the editor window -> the border and titlebar of the dialog flashes/blinks a few times.


Solution

  • The problem arises when the message box has no parent. This works fine for me:

    QMessageBox box(this);
    box.setStandardButtons(QMessageBox::Close);
    box.exec();
    

    But this does not:

    QMessageBox box;
    box.setStandardButtons(QMessageBox::Close);
    box.exec();
    

    This makes sense... the message box can't blink unless it knows that its parent was clicked on.