用过flex编程的童鞋应该用过alert()消息框吧
下面我来介绍Qt中使用过的消息对话框,Q4消息对话框类型多而且功能比较强大。
1.Question消息框:
switch(QMessageBox::question(this,"Question",tr("It's end of document,search from begin?"), QMessageBox::Ok|QMessageBox::Cancel,QMessageBox::Ok)) { case QMessageBox::Ok: label->setText(" Question button / Ok "); break; case QMessageBox::Cancel: label->setText(" Question button / Cancel "); break; default: break; }
QMessageBox::information(this,"Information",tr("anything you want tell user"));
switch(QMessageBox::warning(this,"Warning",tr("Save changes to document?"), QMessageBox::Save|QMessageBox::Discard|QMessageBox::Cancel,QMessageBox::Save)) { case QMessageBox::Save: label->setText(" Warning button / Save "); break; case QMessageBox::Discard: label->setText(" Warning button / Discard "); break; case QMessageBox::Cancel: label->setText(" Warning button / Cancel "); break; default: break; }
QMessageBox::critical(this,"Critical",tr("tell user a critical error")); label->setText(" Critical MessageBox ");
QMessageBox::about(this,"About",tr("Message box example!")); label->setText(" About MessageBox ");
QMessageBox::aboutQt(this,"About Qt"); label->setText(" About Qt MessageBox ");
QMessageBox customMsgBox; customMsgBox.setWindowTitle("Custom message box"); QPushButton *lockButton = customMsgBox.addButton(tr("Lock"),QMessageBox::ActionRole); QPushButton *unlockButton = customMsgBox.addButton(tr("Unlock"),QMessageBox::ActionRole); QPushButton *cancelButton = customMsgBox.addButton(QMessageBox::Cancel); customMsgBox.setIconPixmap(QPixmap(":/images/linuxredhat.png")); customMsgBox.setText(tr("This is a custom message box")); customMsgBox.exec(); if(customMsgBox.clickedButton() == lockButton) label->setText(" Custom MessageBox / Lock "); if(customMsgBox.clickedButton() == unlockButton) label->setText(" Custom MessageBox / Unlock "); if(customMsgBox.clickedButton() == cancelButton) label->setText(" Custom MessageBox / Cancel ");
注意: 在使用的过程中,最前面不要忘了添加
#include <QtGui>