Qt World Summit: Submit your Presentation

QMessageBox wanders across the screen

  • Hi

    I have the following code in an application using Qt5.1.1 under Mac OS X 10.8.5. This is to bring up a small message box and accept one of two input alternatives. The box which appears is in a different place on the screen each time. It walks downwards and to the right on each invocation. Does anyone know why this happens and/or how to stop it wandering? The same code run under Linux (F19) keeps the message box in the same place.

    #include <QMessageBox>
    #include "mainwindow.h"

    void MainWindow::step(Grid *p)
    reDraw(p); // display latest position

    QString msgstr = tr("After %1.").arg(strategyName);
    statusBar()->showMessage(msgstr); // show strategy used
    QMessageBox step(0,
    tr(" Step?"),
    QMessageBox::Ok |QMessageBox::Default,

    if (step.exec() == QMessageBox::Cancel)

  • It's probably an OS behavior that tries to show it in the "most proper area", which might change every time. Not a big deal, it's always meant to appear on the user visible screen, no matter where.
    Anyway, if you really want to set it to appear somewhere specific you may claculate the xPos, yPos coordinates and then use QMessageBox::move(xPos, yPos);

  • Thanks T3sty. As it was platform dependent I assumed there was a good reason for the strange behaviour. Odd that the position is progressive though.

    I'll give the ::move() method a try.

    Thanks again.

  • ... except QMessageBox doesn't have a function move according to Assistant ...

  • Whoops! Yes it does. I beg your pardon.

  • No problem :)
    For people who are not familiar with this, the move() function is inherited from the main QWidget class, and it's a common function for all the widgets in Qt.

  • However, when I inserted a line:


    in front of the
    if (step.exec() == QMessageBox::Cancel)

    it had no effect on the behaviour of the QMessageBox window.

    So no further forward.

Log in to reply