提示、警告、错误、询问、关于对话框

来源:互联网 发布:淘宝网男鞋货到付款 编辑:程序博客网 时间:2024/06/03 22:56

QMessageBox类提供了常用的弹出式对话框:提示、警告、错误、询问、关于对话框

原型如下:

[cpp] view plain copy
  1. void    about ( QWidget * parent, const QString & title, const QString & text )  
  2. void    aboutQt ( QWidget * parent, const QString & title = QString() )  
  3. StandardButton  critical ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )  
  4. StandardButton  information ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )  
  5. StandardButton  question ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )  
  6. StandardButton  warning ( QWidget * parent, const QString & title, const QString & text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton )  


(1)title表示标题

(2)text是对话框中的内容信息

(3)buttons是要显示的按钮,它可以是:

[cpp] view plain copy
  1. NoButton           = 0x00000000,  
  2. Ok                 = 0x00000400,  
  3. Save               = 0x00000800,  
  4. SaveAll            = 0x00001000,  
  5. Open               = 0x00002000,  
  6. Yes                = 0x00004000,  
  7. YesToAll           = 0x00008000,  
  8. No                 = 0x00010000,  
  9. NoToAll            = 0x00020000,  
  10. Abort              = 0x00040000,  
  11. Retry              = 0x00080000,  
  12. Ignore             = 0x00100000,  
  13. Close              = 0x00200000,  
  14. Cancel             = 0x00400000,  
  15. Discard            = 0x00800000,  
  16. Help               = 0x01000000,  
  17. Apply              = 0x02000000,  
  18. Reset              = 0x04000000,  
  19. RestoreDefaults    = 0x08000000,  


(4)defaultButton是默认激活的按钮

(5)对话框的返回值即是用户最终点击的按钮。

 

示例:

[cpp] view plain copy
  1. void Dialog::criticalMessage()  
  2. {  
  3.     QMessageBox::StandardButton reply;  
  4.     reply = QMessageBox::critical(this, tr("QMessageBox::critical()"),  
  5.                                     MESSAGE,  
  6.                                     QMessageBox::Abort | QMessageBox::Retry | QMessageBox::Ignore);  
  7.     if (reply == QMessageBox::Abort)  
  8.         criticalLabel->setText(tr("Abort"));  
  9.     else if (reply == QMessageBox::Retry)  
  10.         criticalLabel->setText(tr("Retry"));  
  11.     else  
  12.         criticalLabel->setText(tr("Ignore"));  
  13. }  
  14.   
  15. void Dialog::informationMessage()  
  16. {  
  17.     QMessageBox::StandardButton reply;  
  18.     reply = QMessageBox::information(this, tr("QMessageBox::information()"), MESSAGE);  
  19.     if (reply == QMessageBox::Ok)  
  20.         informationLabel->setText(tr("OK"));  
  21.     else  
  22.         informationLabel->setText(tr("Escape"));  
  23. }  
  24.   
  25. void Dialog::questionMessage()  
  26. {  
  27.     QMessageBox::StandardButton reply;  
  28.     reply = QMessageBox::question(this, tr("QMessageBox::question()"),  
  29.                                     MESSAGE,  
  30.                                     QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);  
  31.     if (reply == QMessageBox::Yes)  
  32.         questionLabel->setText(tr("Yes"));  
  33.     else if (reply == QMessageBox::No)  
  34.         questionLabel->setText(tr("No"));  
  35.     else  
  36.         questionLabel->setText(tr("Cancel"));  
  37. }  
  38.   
  39. void Dialog::warningMessage()  
  40. {  
  41.     QMessageBox msgBox(QMessageBox::Warning, tr("QMessageBox::warning()"),  
  42.                        MESSAGE, 0, this);  
  43.     msgBox.addButton(tr("Save &Again"), QMessageBox::AcceptRole);  
  44.     msgBox.addButton(tr("&Continue"), QMessageBox::RejectRole);  
  45.     if (msgBox.exec() == QMessageBox::AcceptRole)  
  46.         warningLabel->setText(tr("Save Again"));  
  47.     else  
  48.         warningLabel->setText(tr("Continue"));  
  49.   
  50. }  


界面:

0 0
原创粉丝点击