Search code examples
c++qtqmessagebox

Qt QMessageBox center 'critical' in desktop


The critical method of QMessageBox has 4 overridden methods, it also has individual methods for setting the title, message text and parent.

I want to display a critical error dialog in the center of the desktop. I have the desktop geometry, what I need is the geometry of the message box so I can apply it to the desktop geometry to center the error dialog.

But its a chicken and egg, I can't get the error dialog before it's rendered for positioning, so how do I do this? I don't want to have to resort to magic numbers to apply an offset.


Solution

  • The actual solution was a lot simpler, just removing the parent parameter from the call to critical has the exact desired effect:

        const QString csMsg("\'" + strConfig + "\' does not exist!");
        const QString csTitle("Error");
        QMessageBox msgBox;
        msgBox.critical(nullptr, csTitle, csMsg);
        QApplication::quit();