Qt QMessageBox详解

这篇具有很好参考价值的文章主要介绍了Qt QMessageBox详解。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一.QMessageBox介绍

文本消息显示框(message box)向用户发出情况警报信息并进一步解释警报或向用户提问,以及可选的详细文本以在用户请求时提供更多数据。

枚举

枚举 枚举值
enum ButtonRole { InvalidRole, AcceptRole, RejectRole, DestructiveRole, ActionRole, …, ResetRole }
enum Icon { NoIcon, Question, Information, Warning, Critical }
enum StandardButton { Ok, Open, Save, Cancel, Close, …, ButtonMask }
flags StandardButtons

属性

属性名称 类型
detailedText QString
standardButtons StandardButtons
icon Icon
iconPixmap QPixmap
informativeText QString
text QString
textFormat Qt::TextFormat
textInteractionFlags Qt::TextInteractionFlags

函数

返回值 函数原型 说明
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(QWidget *parent = nullptr) 构造函数
virtual ~QMessageBox() 析构函数
void addButton(QAbstractButton *button, QMessageBox::ButtonRole role) 添加按钮到消息框中
QPushButton * addButton(const QString &text, QMessageBox::ButtonRole role) 添加按钮到消息框中
QPushButton * addButton(QMessageBox::StandardButton button) 添加按钮到消息框中
QAbstractButton * button(QMessageBox::StandardButton which) const 返回与标准按钮对应的指针,如果此消息框中不存在标准按钮,则返回nullptr
QMessageBox::ButtonRole buttonRole(QAbstractButton *button) const 返回指定按钮的按钮角色。如果按钮为nullptr或尚未添加到消息框,则此函数返回InvalidRole。
QList<QAbstractButton *> buttons() const 返回已添加到消息框中的所有按钮的列表。
QCheckBox * checkBox() const 返回对话框上显示的复选框。如果未设置任何复选框,则为nullptr。
QAbstractButton * clickedButton() const 返回用户单击的按钮,如果用户按Esc键且未设置转义按钮,则返回nullptr。
QPushButton * defaultButton() const 返回应该是消息框默认按钮的按钮。如果未设置默认按钮,则返回nullptr。
QString detailedText() const 保存要显示在详细信息区域中的文本。
QAbstractButton * escapeButton() const 返回按下转义时激活的按钮。
QMessageBox::Icon icon() const 保存消息框的图标
QPixmap iconPixmap() const 保存当前图标
QString informativeText() const 包含为消息提供更完整描述的信息性文本
void open(QObject *receiver, const char *member) 打开对话框并将其 finished()或 buttonClicked()信号连接到接收器和成员指定的插槽。如果slot-in成员的第一个参数有一个指针,则连接到 buttonClicked(),否则连接到finished()。
void removeButton(QAbstractButton *button) 从按钮框中删除按钮而不删除它。
void setCheckBox(QCheckBox *cb) 设置消息对话框上的复选框cb。消息框获取复选框的所有权。参数cb可以是nullptr,以从消息框中删除现有复选框。
void setDefaultButton(QPushButton *button) 将消息框的默认按钮设置为按钮。
void setDefaultButton(QMessageBox::StandardButton button) 将消息框的默认按钮设置为按钮。
void setDetailedText(const QString &text) 保存要显示在详细信息区域中的文本。
void setEscapeButton(QAbstractButton *button) 将按下Escape键时激活的按钮设置为按钮。
void setEscapeButton(QMessageBox::StandardButton button) 将按下Escape键时激活的按钮设置为按钮。
void setIcon(QMessageBox::Icon) 保存消息框的图标
void setIconPixmap(const QPixmap &pixmap) 保存消息框的图标
void setInformativeText(const QString &text) 保存要显示的消息框文本
void setStandardButtons(QMessageBox::StandardButtons buttons) 根据文本格式设置(QMessageBox::textFormat),文本将被解释为纯文本或富文本。默认设置为 Qt::AutoText,,即消息框将尝试自动检测文本的格式。
void setText(const QString &text) 保存要显示的消息框文本
void setTextFormat(Qt::TextFormat format) 保存消息框显示的文本格式
void setTextInteractionFlags(Qt::TextInteractionFlags flags)
void setWindowModality(Qt::WindowModality windowModality)
void setWindowTitle(const QString &title)
QMessageBox::StandardButton standardButton(QAbstractButton *button) const 返回与给定按钮对应的标准按钮枚举值,如果给定按钮不是标准按钮,则返回NoButton。
QMessageBox::StandardButtons standardButtons() const 返回与给定按钮对应的标准按钮枚举值,如果给定按钮不是标准按钮,则返回NoButton。
QString text() const 保存要显示的消息框文本
Qt::TextFormat textFormat() const 保存消息框显示的文本格式
Qt::TextInteractionFlags textInteractionFlags() const

二.QMessageBox的用法

1.导入QMessage库

#include <QMessageBox>

2.弹窗提示

构造QMessageBox的实例,设置所需显示的消息,然后调用exec()来显示消息。用户必须单击“确定”按钮以关闭消息框。GUI的其余部分将被阻止,直到消息框被关闭。

QMessageBox msgBox;
msgBox.setText(u8"你好~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
msgBox.exec();

Qt QMessageBox详解

3.提供选项的弹窗提示

一个比仅仅提醒用户事件更好的方法是询问用户该怎么做。将问题存储在信息性文本属性中,并将标准按钮属性设置为所需的按钮集作为用户响应集。通过使用位或运算符组合StandardButtons中的值来指定按钮。按钮的显示顺序取决于平台。例如,在Windows上,“保存”显示在“取消”的左侧,而在Mac OS上,顺序相反。要向用户提供更多信息以帮助他回答问题,通过setDetailedText设置详细文本属性。如果设置了详细文本属性,则显示详细信息按钮。exec()槽返回单击的按钮的StandardButtons值。

QMessageBox msgBox;
msgBox.setText(u8"该文档已被修改.");
msgBox.setDetailedText(u8"这里是详细的提示信息");
msgBox.setInformativeText(u8"是否要保存你的修改?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();

switch (ret) {
  case QMessageBox::Save:
      qDebug() << "save";
      break;
  case QMessageBox::Discard:
      qDebug() << "Discard";
      break;
  case QMessageBox::Cancel:
      qDebug() << "Cancel";
      break;
  default:
      qDebug() << "default";
      break;
}

Qt QMessageBox详解

4.作为提示,报警,报错提示窗口

int ret = QMessageBox::information(this, tr(u8"信息提示"),
                               tr(u8"该文档已被修改.\n"
                                  u8"是否要保存你的修改?"),
                               QMessageBox::Ok | QMessageBox::Cancel | QMessageBox::Help);

Qt QMessageBox详解

Qt QMessageBox详解
Qt QMessageBox详解
Qt QMessageBox详解
对于窗口的类型,有以下几种可以选择:

内容 说明
QMessageBox::NoIcon 0 the message box does not have any icon.
QMessageBox::Question 4 an icon indicating that the message is asking a question.
QMessageBox::Information 1 an icon indicating that the message is nothing out of the ordinary.
QMessageBox::Warning 2 an icon indicating that the message is a warning, but can be dealt with.
QMessageBox::Critical 3 an icon indicating that the message represents a critical problem.

对于窗口中的按钮,有以下几种可以选择:文章来源地址https://www.toymoban.com/news/detail-439067.html

内容 说明
QMessageBox::Ok 0x00000400 An “OK” button defined with the AcceptRole.
QMessageBox::Open 0x00002000 An “Open” button defined with the AcceptRole.
QMessageBox::Save 0x00000800 A “Save” button defined with the AcceptRole.
QMessageBox::Cancel 0x00400000 A “Cancel” button defined with the RejectRole.
QMessageBox::Close 0x00200000 A “Close” button defined with the RejectRole.
QMessageBox::Discard 0x00800000 A “Discard” or “Don’t Save” button, depending on the platform, defined with the DestructiveRole.
QMessageBox::Apply 0x02000000 An “Apply” button defined with the ApplyRole.
QMessageBox::Reset 0x04000000 A “Reset” button defined with the ResetRole.
QMessageBox::RestoreDefaults 0x08000000 A “Restore Defaults” button defined with the ResetRole.
QMessageBox::Help 0x01000000 A “Help” button defined with the HelpRole.
QMessageBox::SaveAll 0x00001000 A “Save All” button defined with the AcceptRole.
QMessageBox::Yes 0x00004000 A “Yes” button defined with the YesRole.
QMessageBox::YesToAll 0x00008000 A “Yes to All” button defined with the YesRole.
QMessageBox::No 0x00010000 A “No” button defined with the NoRole.
QMessageBox::NoToAll 0x00020000 A “No to All” button defined with the NoRole.
QMessageBox::Abort 0x00040000 An “Abort” button defined with the RejectRole.
QMessageBox::Retry 0x00080000 A “Retry” button defined with the AcceptRole.
QMessageBox::Ignore 0x00100000 An “Ignore” button defined with the AcceptRole.
QMessageBox::NoButton 0x00000000 An invalid button.

到了这里,关于Qt QMessageBox详解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • QT 消息对话框按钮显示

    搞QT嘛,大多数都是军工。都要国产化,而且消息对话框的按钮的英文也不是很得劲,所以需要汉化。使用静态函数的按钮就是显示英文,汉化的代码如下。 QmessageBox功能挺强大的,可以设置默认的按钮,也可以设置点击窗口的关闭按钮实际上点击的是那个按钮,默认情况下

    2024年02月10日
    浏览(35)
  • Qt信号和槽绑定实例,点击pushbutton按钮触发QLabel文本显示和关闭

    功能简介: 点击qt界面中的open按钮在界面中显示文本,同时按钮变为close按钮,再点击close按钮可以关闭显示文本,同时按钮变为open按钮,继续点击open按钮。。。。。。 关键内容: 1.创建一个qt工程 2.定义一个QPuschButton按钮实例和一个QLabel文本显示实例 3.设置按钮显示内容和

    2024年02月12日
    浏览(33)
  • Qt之菜单栏、工具栏、状态栏介绍及工具栏QAction的动态增删显示实现方式

    目的 端应用程序或者编辑器基本都支持工具栏快捷功能的动态增删,即通过在菜单栏上打钩就可以在工具栏上看到相应功能的快捷按钮,取消打钩则在工具栏上就移除了该功能的快捷按钮。那么Qt如何实现这个功能,本篇目的就是记录实现此功能的方法及思路。 效果 先看下

    2024年02月08日
    浏览(35)
  • 96.qt qml-http之XMLHttpRequest介绍详解使用

    在QML中我们可以通过XMLHttpRequest 来实现http/https访问网络接口,接下来我们先来学习XMLHttpRequest类的常用部分、 由于QML的XMLHttpRequest少部分参数是没有的,所以本章来单独讲解下。 下章我们来实现旋转请求按钮以及通用的JSON请求模板方法 首先推荐一个http测试工具httpbin,该服务

    2024年02月08日
    浏览(25)
  • React 消息文本循环展示

    页面上有个小喇叭,循环展示消息内容 设置定时器,修改translateX属性来实现滚动, 判断滚动位置,修改list位置来实现无限滚动 判断了内容不超出,就不滚动 如果内容超出了,但是内容太少,导致没有及时的handleMessage 没有处理这一块的逻辑。解决办法,就是double一下数据

    2024年02月09日
    浏览(21)
  • Qt开发技术:Q3D图表开发笔记(二):Q3DBar三维柱状图介绍、Demo以及代码详解

    若该文为原创文章,转载请注明原文出处 本文章博客地址:https://hpzwl.blog.csdn.net/article/details/130150728 各位读者,知识无穷而人力有穷,要么改需求,要么找专业人士,要么自己研究 红胖子网络科技博文大全:开发技术集合(包含Qt实用技术、树莓派、三维、OpenCV、OpenGL、ff

    2023年04月22日
    浏览(61)
  • Qt开发技术:Q3D图表开发笔记(三):Q3DSurface三维曲面图介绍、Demo以及代码详解

      qt提供了q3d进行三维开发,虽然这个框架没有得到大量运用也不是那么成功,性能上也有很大的欠缺,但是普通的点到为止的应用展示还是可以的。   其中就包括华丽绚烂的三维图表,数据量不大的时候是可以使用的。   前面介绍了基础的q3d散点图、柱状图,本篇介

    2023年04月20日
    浏览(60)
  • 【嵌入式Qt开发入门】如何使用Qt进行文本读写——QFile读写文本

            在很多时候我们需要读写文本文件进行读写,比如写个 Mp3 音乐播放器需要读 Mp3 歌词里的文本,比如修改了一个 txt 文件后保存,就需要对这个文件进行读写操作。本文介绍简单的文本文件读写,内容精简,让大家了解文本读写的基本操作。         QFile 类提

    2024年02月12日
    浏览(33)
  • QMessageBox手动添加按钮并绑定按钮的信号

    视频展示效果(结合代码看效果更佳哦,代码在最下面): QMessageBox手动添加有重试效果的按钮 效果图:  点击详细文本之后展开如下图:   图标可选: QMessageBox::Critical 错误图标 QMessageBox::NoIcon 没有图标 QMessageBox::Question 提问图标 QMessageBox::Information 消息图标 QMessageBox::Wa

    2024年02月16日
    浏览(28)
  • PyQt6 QMessageBox的使用方法

    放置一个QPushButton就可以。 点击按钮显示QMessageBox

    2024年02月11日
    浏览(29)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包