QMessageBox内置了几种static方法,例如
QMessageBox::question()
返回值:StandardButton
参数:QWidget * 父窗口,标题名,内容,按钮 YES|NO,默认选中按钮)
示例:
1 #include "mainwindow.h" 2 #include <QApplication> 3 #include <QMessageBox> 4 int main(int argc, char *argv[]) 5 { 6 QApplication a(argc, argv); 7 MainWindow w; 8 if(QMessageBox::No == QMessageBox::question(&w,"Question","Hello?",QMessageBox::Yes|QMessageBox::No,QMessageBox::Yes)) 9 { 10 QMessageBox::information(&w,"You click No","I want to you click Yes"); 11 } 12 else{ 13 QMessageBox::information(&w,"You click Yes","Good Job"); 14 } 15 w.show(); 16 17 return a.exec(); 18 }
效果:
其他几个static方法使用大同小异,具体可以查阅API
自制对话框
代码:
1 #include "mainwindow.h" 2 #include <QApplication> 3 #include <QMessageBox> 4 int main(int argc, char *argv[]) 5 { 6 QApplication a(argc, argv); 7 MainWindow w; 8 QMessageBox myBox; 9 myBox.setText("This is a box"); 10 myBox.setInformativeText("Are you ok?"); 11 myBox.setDetailedText("Are you ok now"); 12 myBox.setStandardButtons(QMessageBox::Ok|QMessageBox::No|QMessageBox::Help); 13 int answer = myBox.exec(); 14 if(answer == QMessageBox::Ok){ 15 QMessageBox::about(&w,"OK","OK"); 16 } 17 else{ 18 QMessageBox::about(&w,"...","..."); 19 } 20 w.show(); 21 22 return a.exec(); 23 }
效果: