[转]QT中各种MessageBox的使用

推荐一个好的:http://blog.sina.com.cn/s/blog_9d16de8101010r2t.html


MessageBox.h

  1. #ifndef MESSAGEBOX_H  
  2. #define MESSAGEBOX_H  
  3.   
  4. #include <QtGui>  
  5. #include "ui_messagebox.h"  
  6.   
  7. class MessageBox : public QDialog  
  8. {  
  9.     Q_OBJECT  
  10.   
  11. public:  
  12.     MessageBox(QWidget *parent = 0, Qt::WFlags flags = 0);  
  13.     ~MessageBox();  
  14.   
  15. private:  
  16.     Ui::MessageBoxClass ui;  
  17.   
  18.     QLabel *label;  
  19.   
  20.     private slots:  
  21.         void slotQuestion();  
  22.         void slotInformation();  
  23.         void slotWarning();  
  24.         void slotCritical();  
  25.         void slotAbout();  
  26.         void slotAboutQt();  
  27.         void slotCustom();  
  28. };  
  29.   
  30. #endif // MESSAGEBOX_H  

MessageBox.cpp

  1. #include "messagebox.h"  
  2.   
  3. MessageBox::MessageBox(QWidget *parent, Qt::WFlags flags)  
  4.     : QDialog(parent, flags)  
  5. {  
  6.     ui.setupUi(this);  
  7.   
  8.     setWindowTitle(tr("Message Box Example"));  
  9.   
  10.     label = new QLabel;  
  11.   
  12.     QPushButton *btn1 = new QPushButton("Question");  
  13.     QPushButton *btn2 = new QPushButton("Information");  
  14.     QPushButton *btn3 = new QPushButton("Warning");  
  15.     QPushButton *btn4 = new QPushButton("Critical");  
  16.     QPushButton *btn5 = new QPushButton("About");  
  17.     QPushButton *btn6 = new QPushButton("About Qt");  
  18.     QPushButton *btn7 = new QPushButton("Custom");  
  19.   
  20.     QGridLayout *grid = new QGridLayout;  
  21.     grid->addWidget(btn1,0,0);  
  22.     grid->addWidget(btn2,0,1);  
  23.     grid->addWidget(btn3,1,0);  
  24.     grid->addWidget(btn4,1,1);  
  25.     grid->addWidget(btn5,2,0);  
  26.     grid->addWidget(btn6,2,1);  
  27.     grid->addWidget(btn7,3,0);  
  28.   
  29.     QVBoxLayout *mainLayout = new QVBoxLayout;  
  30.     mainLayout->setMargin(10);  
  31.     mainLayout->setSpacing(20);  
  32.     mainLayout->addWidget(label);  
  33.     mainLayout->addLayout(grid);  
  34.     setLayout(mainLayout);  
  35.   
  36.     connect(btn1,SIGNAL(clicked()),this,SLOT(slotQuestion()));  
  37.     connect(btn2,SIGNAL(clicked()),this,SLOT(slotInformation()));  
  38.     connect(btn3,SIGNAL(clicked()),this,SLOT(slotWarning()));  
  39.     connect(btn4,SIGNAL(clicked()),this,SLOT(slotCritical()));  
  40.     connect(btn5,SIGNAL(clicked()),this,SLOT(slotAbout()));  
  41.     connect(btn6,SIGNAL(clicked()),this,SLOT(slotAboutQt()));  
  42.     connect(btn7,SIGNAL(clicked()),this,SLOT(slotCustom()));  
  43. }  
  44.   
  45. MessageBox::~MessageBox()  
  46. {  
  47.   
  48. }  
  49.   
  50. void MessageBox::slotQuestion()  
  51. {  
  52.     switch(QMessageBox::question(this,"Question",tr("It's end of document,search from begin?"),  
  53.         QMessageBox::Ok|QMessageBox::Cancel,QMessageBox::Ok))  
  54.     {  
  55.     case QMessageBox::Ok:  
  56.         label->setText(" Question button / Ok ");  
  57.         break;  
  58.     case QMessageBox::Cancel:  
  59.         label->setText(" Question button / Cancel ");  
  60.         break;  
  61.     default:  
  62.         break;  
  63.     }  
  64.     return;  
  65. }  
  66.   
  67. void MessageBox::slotInformation()  
  68. {  
  69.     QMessageBox::information(this,"Information",tr("anything you want tell user"));  
  70.     return;  
  71. }  
  72.   
  73. void MessageBox::slotWarning()  
  74. {  
  75.     switch(QMessageBox::warning(this,"Warning",tr("Save changes to document?"),  
  76.         QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel,QMessageBox::Save))  
  77.     {  
  78.     case QMessageBox::Save:  
  79.         label->setText(" Warning button / Save ");  
  80.         break;  
  81.     case QMessageBox::Discard:  
  82.         label->setText(" Warning button / Discard ");  
  83.         break;  
  84.     case QMessageBox::Cancel:  
  85.         label->setText(" Warning button / Cancel ");  
  86.         break;  
  87.     default:  
  88.         break;  
  89.     }  
  90.     return;  
  91.   
  92. }  
  93.   
  94. void MessageBox::slotCritical()  
  95. {  
  96.     QMessageBox::critical(this,"Critical",tr("tell user a critical error"));  
  97.     label->setText(" Critical MessageBox ");  
  98.     return;  
  99. }  
  100.   
  101. void MessageBox::slotAbout()  
  102. {  
  103.     QMessageBox::about(this,"About",tr("Message box example!"));  
  104.     label->setText(" About MessageBox ");  
  105.     return;  
  106. }  
  107.   
  108. void MessageBox::slotAboutQt()  
  109. {  
  110.     QMessageBox::aboutQt(this,"About Qt");  
  111.     label->setText(" About Qt MessageBox ");  
  112.     return;  
  113. }  
  114.   
  115. void MessageBox::slotCustom()  
  116. {  
  117.     QMessageBox customMsgBox;  
  118.     customMsgBox.setWindowTitle("Custom message box");  
  119.     QPushButton *lockButton = customMsgBox.addButton(tr("Lock"),QMessageBox::ActionRole);  
  120.     QPushButton *unlockButton = customMsgBox.addButton(tr("Unlock"),QMessageBox::ActionRole);  
  121.     QPushButton *cancelButton = customMsgBox.addButton(QMessageBox::Cancel);  
  122.     customMsgBox.setIconPixmap(QPixmap(":/images/linuxredhat.png"));  
  123.     customMsgBox.setText(tr("This is a custom message box"));  
  124.     customMsgBox.exec();  
  125.   
  126.     if(customMsgBox.clickedButton() == lockButton)  
  127.         label->setText(" Custom MessageBox / Lock ");  
  128.     if(customMsgBox.clickedButton() == unlockButton)  
  129.         label->setText(" Custom MessageBox / Unlock ");  
  130.     if(customMsgBox.clickedButton() == cancelButton)  
  131.         label->setText(" Custom MessageBox / Cancel ");  
  132.   
  133.     return;  
  134. }  

main.cpp

  1. #include "messagebox.h"  
  2. #include <QtGui/QApplication>  
  3.   
  4. int main(int argc, char *argv[])  
  5. {  
  6.     QApplication a(argc, argv);  
  7.     MessageBox *w=new MessageBox;  
  8.     w->show();  
  9.     return a.exec();  
  10. }  

效果图:

image

imageimageimageimageimageimage

image



/文章为转载

感谢Rollen Holt


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值