Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#157 Can't move a qmessagebox

unstable
open
nobody
None
2014-03-18
2012-02-10
Vordok
No

In my app if the user forget to enter a data it sends a message using a qmessagebox, but it always appears on 0,0 and I want it in the middle of the screen:

int MainWindow::messageBox( QString button, QMessageBox::ButtonRole buttons, QString info, QMessageBox::Icon icon )
{
QFont f;
QDesktopWidget win = new QDesktopWidget();
QMessageBox
message = new QMessageBox(this);

f.setPointSize(6);

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

return message->exec();

}

I also tried this

int MainWindow::messageBox( QString button, QMessageBox::ButtonRole buttons, QString info, QMessageBox::Icon icon )
{
QFont f;
QMessageBox *message = new QMessageBox(this);

f.setPointSize(6);

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();

}

and this:

int MainWindow::messageBox( QString button, QMessageBox::ButtonRole buttons, QString info, QMessageBox::Icon icon )
{
QFont f;
QDesktopWidget win = new QDesktopWidget();
QMessageBox
message = new QMessageBox(this);

f.setPointSize(6);

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

return message->exec();

}

And none of them work, is this a bug, or am I missing something.

Thanks in advance

Discussion

  • Vordok
    Vordok
    2012-02-10

     
  • Alexander
    Alexander
    2012-02-15

    Try this nice msgbox:


    QMessageBox box(icon, title, text);
    box.setWindowModality(Qt::WindowModal);
    box.setWindowFlags(Qt::ToolTip);
    box.setStyleSheet("QDialog{ border: 1px solid black; border-style: solid; border-radius: 4px; }");
    box.exec();

     
  • discus-axel
    discus-axel
    2012-02-27

    Hi,

    if you add a show statement before your move statement it should work. I found this workaround with the help of google.

    ...
    message->show();
    message->move( win->width() / 2, win->height() / 2 );
    return message->exec();

    I think that is a bug, because normally a QMessageBox is centered over its parent under Qt/X11.