在Qt中經常需要彈出窗口,QMessageBox可以實現此功能,一共有三種窗口,information, question, 和 warning,critical, about分別對應感嘆號,問號和叉號等等,使用方法很簡單,一共有三個參數,第一個是父窗口句柄,剩下兩個分別為窗口名稱和顯示內容,顯示內容為QString類型。如下所示:
QMessageBox::information(this, tr("Title"), tr("Content")); QMessageBox::question(this, tr("Title"), tr("Content")); QMessageBox::warning(this, tr("Title"), tr("Content")); QMessageBox::critical(this, tr("Title"), tr("Content")); QMessageBox::about(this, tr("Title"), tr("Content"));
如果需要對話框下面有Ok, Yes, No 等按鈕,需要添加后面兩個參數,一個是出現哪兩個按鈕,一個是設置缺省按鈕:
QMessageBox::StandardButton r = QMessageBox::question(this, "Title", "Content", QMessageBox::Yes | QMessageBox::No, QMessageBox::No); if (r == QMessageBox::Yes) { // Do something here }