Qt学习 第36节:QMessageBox

QMessageBox(QMessageBox::Icon icon, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = NoButton, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint)

使用QMessageBox有两种方式,一种是基于The Property-based API,另外一种是the static functions,偏向使用静态函数。

1.The Property-based API

实例化一个QMessageBox,设置属性,再调用exec()来显示消息提示框

QMessageBox msgBox;
msgBox.setText("The document has been modified.");
msgBox.exec();

当使用一个带有标准按钮的消息提示框时,msgBox.exec()返回标准按钮的值,显示哪个标准被点击
当使用一个自定义按钮的消息提示框时,msgBox.exec()返回一个不透明值,使用clickedButton()来决定哪一个按钮被点击

更一步改进

给用户提示信息使用setInformativeText,使用标准按钮设置方法setStandardButtons来设置StandardButton,另外设置一个默认按钮(弹出提示框,你按下enter默认选中的那个按钮),当用户点击按钮时,按钮的行为被ButtonRole定义,标准按钮的ButtonRole系统已经预定好了

QMessageBox msgBox;
msgBox.setText("The document has been modified.");
msgBox.setInformativeText("Do you want to save your changes?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);

int ret = msgBox.exec();//返回用户点击对应标准按钮的枚举值

switch (ret) {
  case QMessageBox::Save:
      // Save was clicked
      break;
  case QMessageBox::Discard:
      // Don't Save was clicked
      break;
  case QMessageBox::Cancel:
      // Cancel was clicked
      break;
  default:
      // should never be reached
      break;
}

另外还可以使用setDetailedText方法,对提示内容做更进一步提示

 提醒:详细信息只支持纯文本, The main text and informative text properties 可以带超链接

Qt提供了四种预设的严重级别消息提示框,他们唯一的区别就是消息提示图标不同,你可以使用setIcon()来选择设置标准图标,你也可是使用setIconPixmap()设置你自己的图标

QMessageBox Class | Qt Widgets 5.15.8https://doc.qt.io/qt-5/qmessagebox.html#

  QMessageBox Class | Qt Widgets 5.15.8https://doc.qt.io/qt-5/qmessagebox.html#ButtonRole-enum

QMessageBox Class | Qt Widgets 5.15.8https://doc.qt.io/qt-5/qmessagebox.html#addButtonQMessageBox Class | Qt Widgets 5.15.8https://doc.qt.io/qt-5/qmessagebox.html#StandardButton-enum

2.静态方法的使用方法:没别的就是方便

void about(QWidget *parent, const QString &title, const QString &text)
//(父类指针,窗口标题,提示文本)
void aboutQt(QWidget *parent, const QString &title = QString())
//(父类指针,窗口标题)
QMessageBox::StandardButton critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)
//错误提示(父类指针,窗口标题,提示文本,OK按钮,默认按钮(默认无))
QMessageBox::StandardButton information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)
//信息(父类指针,窗口标题,提示文本,OK按钮,默认按钮(默认无))
QMessageBox::StandardButton question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = StandardButtons(Yes | No), QMessageBox::StandardButton defaultButton = NoButton)
//询问(父类指针,窗口标题,提示文本,Yes|No按钮,默认按钮(默认无))
QMessageBox::StandardButton warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)
//警告(父类指针,窗口标题,提示文本,OK按钮,默认按钮(默认无))

一般用法

Static functions are available for creating information(), question(), warning(), and critical() message boxes.

  int ret = QMessageBox::warning(this, tr("My Application"),
                                 tr("The document has been modified.\n"
                                    "Do you want to save your changes?"),
                                 QMessageBox::Save | QMessageBox::Discard
                                 | QMessageBox::Cancel,
                                 QMessageBox::Save);

高级用法

如果自带StandardButton不满意,还可以自定义setButton()方法自己定义按钮,语法

void QMessageBox::addButton(QAbstractButton *buttonQMessageBox::ButtonRole role)

QPushButton *QMessageBox::addButton(const QString &textQMessageBox::ButtonRole role)

QPushButton *QMessageBox::addButton(QMessageBox::StandardButton button)

Custom QMessageBox Buttons - Qt Wikihttps://wiki.qt.io/Custom_QMessageBox_Buttons

QMessageBox msgBox;
QPushButton *connectButton = msgBox.addButton(tr("Connect"), QMessageBox::ActionRole);
QPushButton *abortButton = msgBox.addButton(QMessageBox::Abort);

msgBox.exec();

if (msgBox.clickedButton() == connectButton) 
{
    // connect
} else if (msgBox.clickedButton() == abortButton) 
{
    // abort
}

QMessageBox msgBox;
msgBox.setText(tr("Confirm?"));
QAbstractButton* pButtonYes = msgBox.addButton(tr("Yeah!"), QMessageBox::YesRole);
msgBox.addButton(tr("Nope"), QMessageBox::NoRole);
msgBox.exec();

if (msgBox.clickedButton()==pButtonYes) {

   //Execute command
}

最介绍一个默认按键enter和esc的默认行为

void QMessageBox::setDefaultButton(QPushButton *button)

void QMessageBox::setDefaultButton(QMessageBox::StandardButton button)

void QMessageBox::setEscapeButton(QAbstractButton *button)

void QMessageBox::setEscapeButton(QMessageBox::StandardButton button)

当使用一个带有标准按钮的消息提示框时,msgBox.exec()返回标准按钮的值,显示哪个标准被点击
当使用一个自定义按钮的消息提示框时,msgBox.exec()返回一个不透明值,使用clickedButton()来决定哪一个按钮被点击

使用标准按钮消息提示框

QMessageBox msgbox(QMessageBox::Critical, tr("Tile"), tr("text"), QMessageBox::Save | QMessageBox::Discard |QMessageBox::Cancel , this, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint);
qDebug()<< msgbox.exec();
qDebug()<< static_cast< QMessageBox::StandardButton >(msgbox.exec());

QMessageBox msgBox(this);
msgBox.setWindowTitle(tr("Tile"));
msgBox.setIcon(QMessageBox::Critical);
msgBox.setText(tr("text"));

msgBox.setInformativeText(tr("Do you want to save your changes?"));
msgBox.setDetailedText(tr("This property holds the text to be displayed in the details area.The text will be interpreted as a plain text.By default, this property contains an empty string."));
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard |QMessageBox::Cancel);

msgBox.setDefaultButton(QMessageBox::Save);
msgBox.setEscapeButton(QMessageBox::Cancel);

qDebug()<< msgBox.exec();
qDebug()<< static_cast< QMessageBox::StandardButton >(msgBox.exec());

使用自定义按钮的消息提示框

QMessageBox msgBox;
msgBox.setWindowTitle(tr("这是标题"));
msgBox.setIcon(QMessageBox::Critical);
msgBox.setText(tr("The document has been modified."));
msgBox.setInformativeText(tr("Do you want to save your changes?"));
msgBox.setDetailedText(tr("This property holds the text to be displayed in the details area.The text will be interpreted as a plain text.By default, this property contains an empty string."));

QPushButton *ok = msgBox.addButton(tr("ok"), QMessageBox::ActionRole);

QPushButton *cancel = msgBox.addButton(QMessageBox::Cancel);

msgBox.setDefaultButton(ok);//按下enter键,相当于点击了OK键
msgBox.setEscapeButton(cancel);

qDebug()<< msgBox.exec();//exec()返回0,表示被正常执行

if (msgBox.clickedButton() == ok)
{
    QMessageBox::StandardButton button = QMessageBox::information(&msgBox, "title", "你点击了OK", QMessageBox::Ok|QMessageBox::No, QMessageBox::Ok);
    qDebug()<< button;
}
else if (msgBox.clickedButton() == cancel)
{
    qDebug()<<"cancel";
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值