qt MessageBOX 消息
void MessageBox::slotQuestion() { 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; } return; } void MessageBox::slotInformation() { QMessageBox::information(this,"Information",tr("anything you want tell user")); return; } void MessageBox::slotWarning() { 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; } return; } void MessageBox::slotCritical() { QMessageBox::critical(this,"Critical",tr("tell user a critical error")); label->setText(" Critical MessageBox "); return; } void MessageBox::slotAbout() { QMessageBox::about(this,"About",tr("Message box example!")); label->setText(" About MessageBox "); return; } void MessageBox::slotAboutQt() { QMessageBox::aboutQt(this,"About Qt"); label->setText(" About Qt MessageBox "); return; } void MessageBox::slotCustom() { 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 "); return; }
main.cpp
#include "messagebox.h" #include <QtGui/QApplication> int main(int argc, char *argv[]) { QApplication a(argc, argv); MessageBox *w=new MessageBox; w->show(); return a.exec(); }
效果图:
坚持就是胜利