QT學習三 標準對話框 QMessageBox
阿新 • • 發佈:2019-03-15
stand ESS 自制 標題 示例 lse wan nbsp sage
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 }
效果:
QT學習三 標準對話框 QMessageBox