Search code examples
androidc++qtqmessagebox

Issue with qmessagebox, can't change its position


I have this

int MainWindow::messageBox( QString button, QMessageBox::ButtonRole buttons, QString info, QMessageBox::Icon icon )
{
    QFont f;

    f.setPointSize(6);

    QMessageBox *message = new QMessageBox(this);
    message->setWindowModality(Qt::WindowModal);
    message->setFont(f);
    message->setText(info);
    message->addButton( button, buttons );
    message->setWindowTitle("MainWindow");
    message->setIcon(icon);
    message->move( this->width() / 2, this->height() / 2 );

    return message->exec();
}

But I can't make the qmessagebox go to the center of the screen, I also tried using setGeometry, but it doesn't work. Any ideas on this?


Solution

  • I solved using show() before moving it. This is the code:

    int MainWindow::messageBox( QString button, QMessageBox::ButtonRole buttons, QString info, QMessageBox::Icon icon )
    {
        QFont f;
        QMessageBox *message = new QMessageBox(this);
        QDesktopWidget *win = new QDesktopWidget();
    
        f.setPointSize(6);
    
        message->setWindowModality(Qt::WindowModal);
        message->setFont(f);
        message->setText(info);
        message->addButton( button, buttons );
        message->setWindowTitle("MainWindow");
        message->setIcon(icon);
        message->show();
        message->move( win->width() / 2 - message->width() / 2, win->height() / 2 - message->height() / 2 );
    
        return message->exec();
    }