QMesageBox的使用

一、使用构造函数弹出对话框

1、 QMessageBox msgBox;//最简单的对话框,里面什么也没有

  QString str = “test”;

  msgBox.setText(str);

  msgBox.exec();

2、

  QMessageBox message(QMessageBox::NoIcon, "Title", "Content with icon."); //加入了图片的对话框
  message.setIconPixmap(QPixmap("icon.png"));
  message.exec();

二、使用QMessageBox的静态方法

  1、消息对话框,函数原型:StandardButton QMessageBox::information(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = Ok, StandardButton   defaultButton = NoButton)
  QMessageBox::information(NULL, "Title", "Content", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);//
  2、错误对话框,函数原型:StandardButton QMessageBox::critical(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = Ok,  StandardButton defaultButton = NoButton)
  QMessageBox::critical(NULL, "critical", "Content", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
  3、警告对话框,函数原型:StandardButton QMessageBox::warning(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton)
  QMessageBox::warning(NULL, "warning", "Content", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
  4、问题对话框,函数原型:StandardButton QMessageBox::question(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = StandardButtons( Yes | No ), StandardButton defaultButton = NoButton)
  QMessageBox::question(NULL, "question", "Content", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
  5、关于对话框,函数原型:void QMessageBox::about(QWidget *parent, const QString &title, const QString &text)

    QMessageBox::about(NULL, "About", "About this application");

三、如何对二中的返回值做判断

  1~4函数都是返回StandardButton,所以:

QMessageBox::StandardButton rb = QMessageBox::question(NULL, "Show Qt", "Do you want to show Qt dialog?", QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
if(rb == QMessageBox::Yes)
  QMessageBox::aboutQt(NULL, "About Qt");

posted @ 2017-04-05 14:03  朱小勇  阅读(633)  评论(0编辑  收藏  举报