2013-02-05 38 views

回答

3

你需要指定父窗口为QMessageBox

QApplication a(argc, argv); 
qt_test_dialog w; 
w.show(); 
// with additional button 
// QMessageBox box(QMessageBox::Information, "Title", "Hello there!", QMessageBox::Ok); 

// without additional button! 
QMessageBox box(QMessageBox::Information, "Title", "Hello there!", QMessageBox::Ok, &w); 

或者干脆:

QMessageBox box(&w); 
box.setText("Hello"); 
box.exec(); 

注意,父参数甚至可以是空QWidget

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 
    // plain wrong (you will not be able to exit application) - but it demonstrates 
    // the case 
    QMessageBox box(new QWidget()); 
    box.setText("Hello"); 
    box.exec(); 
    return a.exec(); 
} 
+0

什么绝qt_test_dialog是?只是一个自定义的QWidget?你可以请详细解释一下吗? –

+0

@AndreyChernukha Chernukha在我的情况下,这是一个简单的QMainWindow子类(由向导生成),但它可以是自定义的QWidget等。 –

+0

@AndreyChernukha请参阅我的编辑以获取更多说明。 –