2012-02-10 78 views
0

我有這個問題與QMessageBox提示,也無法改變其位置

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

但我不能讓QMessageBox提示轉到屏幕的中心,我也嘗試過使用setGeometry,但它不工作。對此有何想法?

回答

1

我在移動它之前使用show()解決了它。這是代碼:

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

A QMessageBox使用窗口標記Qt::Dialog(和間接地,Qt::Window)創建。這意味着它將被視爲系統窗口,即使它已經分配了父項。當您撥打move()時,它將被定位在桌面座標中。

當您在上面的代碼中移動消息框時,您告訴它出現在桌面座標等於偏離原點(桌面左上角)的主應用程序窗口大小的一半寬度和高度處。

如果您的主應用程序窗口的大小爲400x200,那麼無論您的主應用程序窗口位於何處,您的消息框都會出現在桌面座標200,100處。

如果您將應用程序窗口全屏顯示,然後顯示消息框,則消息框應該(大致)出現在桌面顯示的中心。我粗略地說是因爲你指定了消息框左上角的位置,而不是消息框中心的位置。

如果您希望消息框總是出現在屏幕的中心,那麼您需要使用QDesktopWidget提供的信息來確定正確的屏幕座標應該是什麼。

+0

'QDesktopWidget win; message-> move(win.width()/ 2,win.height()/ 2);'我使用它,但它仍然出現在0,0。我在想這可能是一個必需品bug – Vordok 2012-02-10 22:51:27

+0

糟糕。沒有注意到Android標籤:) – 2012-02-10 23:04:52