QMessageBox Class
QMessageBox 类提供了一个模式对话框,用于通知用户或向用户提问并接收答案。更多
Header: | #include <QMessageBox> |
CMake.QMessageBox | find_package(Qt6 REQUIRED COMPONENTS Widgets) target_link_libraries(mytarget PRIVATE Qt6::Widgets) |
qmake: | QT += widgets |
继承: | QDialog |
- 所有成员(包括继承成员)的列表
- 已废弃成员
- QMessageBox 是标准对话框的一部分。
公共类型
enum | ButtonRole { InvalidRole, AcceptRole, RejectRole, DestructiveRole, ActionRole, …, ResetRole } |
enum | Icon { NoIcon, Question, Information, Warning, Critical } |
(since 6.6) enum class | Option { DontUseNativeDialog } |
flags | Options |
enum | StandardButton { Ok, Open, Save, Cancel, Close, …, ButtonMask } |
flags | StandardButtons |
属性
|
|
公共功能
QMessageBox(QWidget *parent = nullptr) | |
QMessageBox(QMessageBox::Icon icon, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = NoButton, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint) | |
virtual | ~QMessageBox() |
void | addButton(QAbstractButton *button, QMessageBox::ButtonRole role) |
QPushButton * | addButton(QMessageBox::StandardButton button) |
QPushButton * | addButton(const QString &text, QMessageBox::ButtonRole role) |
QAbstractButton * | button(QMessageBox::StandardButton which) const |
QMessageBox::ButtonRole | buttonRole(QAbstractButton *button) const |
QList<QAbstractButton *> | buttons() const |
QCheckBox * | checkBox() const |
QAbstractButton * | clickedButton() const |
QPushButton * | defaultButton() const |
QString | detailedText() const |
QAbstractButton * | escapeButton() const |
QMessageBox::Icon | icon() const |
QPixmap | iconPixmap() const |
QString | informativeText() const |
void | open(QObject *receiver, const char *member) |
QMessageBox::Options | options() const |
void | removeButton(QAbstractButton *button) |
void | setCheckBox(QCheckBox *cb) |
void | setDefaultButton(QMessageBox::StandardButton button) |
void | setDefaultButton(QPushButton *button) |
void | setDetailedText(const QString &text) |
void | setEscapeButton(QAbstractButton *button) |
void | setEscapeButton(QMessageBox::StandardButton button) |
void | setIcon(QMessageBox::Icon) |
void | setIconPixmap(const QPixmap &pixmap) |
void | setInformativeText(const QString &text) |
(since 6.6) void | setOption(QMessageBox::Option option, bool on = true) |
void | setOptions(QMessageBox::Options options) |
void | setStandardButtons(QMessageBox::StandardButtons buttons) |
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 |
QMessageBox::StandardButtons | standardButtons() const |
(since 6.6) bool | testOption(QMessageBox::Option option) const |
QString | text() const |
Qt::TextFormat | textFormat() const |
Qt::TextInteractionFlags | textInteractionFlags() const |
公共插槽
virtual int | exec() override |
信号
void | buttonClicked(QAbstractButton *button) |
静态公共成员
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) |
QMessageBox::StandardButton | information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton) |
QMessageBox::StandardButton | question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = StandardButtons(Yes | No), QMessageBox::StandardButton defaultButton = NoButton) |
QMessageBox::StandardButton | warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton) |
重新实现的保护函数
virtual void | changeEvent(QEvent *ev) override |
virtual void | closeEvent(QCloseEvent *e) override |
virtual bool | event(QEvent *e) override |
virtual void | keyPressEvent(QKeyEvent *e) override |
virtual void | resizeEvent(QResizeEvent *event) override |
virtual void | showEvent(QShowEvent *e) override |
宏
QT_REQUIRE_VERSION(int argc, char **argv, const char *version) |
详细说明
消息框主要显示text ,以提醒用户注意某种情况;显示informative text ,以进一步解释情况;如果用户要求,还可显示detailed text ,以提供更多数据。
消息框还可以显示icon 和standard buttons ,以接受用户的回复。
为使用 QMessageBox 提供了两种 API:基于属性的 API 和静态函数。调用其中一个静态函数是更简单的方法,但它不如使用基于属性的 API 灵活,而且结果信息量也较少。建议使用基于属性的 API。
基于属性的 API
要使用基于属性的 API,请构建一个 QMessageBox 实例,设置所需的属性,然后调用exec() 来显示消息。最简单的配置是只设置message text 属性。
QMessageBox msgBox; msgBox.setText("The document has been modified."); msgBox.exec();
用户必须单击OK 按钮才能取消消息框。图形用户界面的其他部分将被阻止,直到消息框被取消。
比仅仅提醒用户注意事件更好的方法是询问用户如何处理该事件。
将standard buttons 属性设置为您希望作为用户响应的按钮集。这些按钮是通过使用位操作 OR 运算符组合StandardButtons 中的值指定的。按钮的显示顺序与平台有关。例如,在 Windows 上,Save 显示在Cancel 的左边,而在 macOS 上,显示顺序则相反。将其中一个标准按钮标记为default button 。
informative text 属性可用于添加额外的上下文,帮助用户选择适当的操作。
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();
exec() 槽会返回被点击按钮的StandardButtons 值。
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; }
要为用户提供更多信息,帮助他们选择适当的操作,可设置detailed text 属性。根据平台的不同,detailed text ,可能需要用户点击Show Details... 按钮才能显示。
单击Show Details... 按钮可显示详细文本。
富文本和文本格式属性
detailed text 属性始终被解释为纯文本。main text 和informative text 属性既可以是纯文本,也可以是富文本。这些字符串根据text format 属性的设置进行解释。默认设置为auto-text 。
请注意,对于某些包含 XML 元字符的纯文本字符串,自动文本rich text detection test 可能会失败,导致纯文本字符串被错误地解释为富文本。在这些罕见的情况下,请使用Qt::convertFromPlainText() 将纯文本字符串转换为视觉效果相当的富文本字符串,或使用setTextFormat() 明确设置text format 属性。
严重程度级别以及图标和像素图属性
QMessageBox 支持四种预定义的消息严重性级别或消息类型,它们的区别仅在于各自显示的预定义图标。通过将icon 属性设置为predefined icons 来指定四种预定义消息类型之一。以下规则是指导原则:
Question | 用于在正常运行期间提问。 | |
Information | 用于报告有关正常操作的信息。 | |
Warning | 用于报告非关键错误。 | |
Critical | 用于报告严重错误。 |
Predefined icons 这些规则不是由 QMessageBox 定义的,而是由样式提供的。默认值是 。除此之外,所有情况下的消息框都是一样的。使用标准图标时,请使用表中推荐的图标,或使用平台样式指南推荐的图标。如果没有一个标准图标适合您的消息框,您可以通过设置 属性而不是设置 属性来使用自定义图标。No Icon icon pixmap icon
总之,要设置图标,请使用setIcon() 设置标准图标,或使用setIconPixmap() 设置自定义图标。
静态函数 API
使用静态函数 API 构建消息框虽然方便,但不如使用基于属性的 API 灵活,因为静态函数签名缺少用于设置informative text 和detailed text 属性的参数。解决这个问题的一个办法是使用title
参数作为消息框的主文本,使用text
参数作为消息框的信息文本。这样做的明显缺点是信息框的可读性较差,因此平台指南并不推荐这样做。Microsoft Windows 用户界面指南》建议使用application name 作为window's title ,也就是说,如果除了主文本外还有信息文本,则必须将其连接到text
参数。
请注意,静态函数签名中的按钮参数已发生变化,现在用于设置standard buttons 和default button 。
静态函数可用于创建information(),question(),warning() 和critical() 消息框。
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);
标准对话框示例展示了如何使用 QMessageBox 和其他内置 Qt 对话框。
高级用法
如果standard buttons 对您的消息框来说不够灵活,您可以使用addButton() 重载,它接受一个文本和一个ButtonRole 来添加自定义按钮。QMessageBox 会使用ButtonRole 来确定按钮在屏幕上的排序(根据平台而不同)。您可以在调用exec() 后测试clickedButton() 的值。例如
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 }
默认键和 Escape 键
可以使用setDefaultButton() 指定默认按钮(即按下Enter 时激活的按钮)。如果未指定默认按钮,QMessageBox 会尝试根据消息框中使用的按钮button roles 查找默认按钮。
可以使用setEscapeButton() 指定转义按钮(按下Esc 时激活的按钮)。如果没有指定转义按钮,QMessageBox 会尝试使用这些规则找到一个:
- 如果只有一个按钮,则是按下Esc 时激活的按钮。
- 如果有Cancel 按钮,则它是按下Esc 时激活的按钮。
- 如果正好有一个具有the Reject role 或the No role 的按钮,则当按下Esc 时,它将被激活。
当使用这些规则无法确定逃逸按钮时,按Esc 没有任何作用。
另请参阅 QDialogButtonBox 和标准对话框示例。
成员类型文件
enum QMessageBox::ButtonRole
该枚举描述了可用于描述按钮框中按钮的角色。这些角色的组合就像标志一样,用于描述按钮行为的不同方面。
常量 | 值 | 描述 |
---|---|---|
QMessageBox::InvalidRole | -1 | 按钮无效。 |
QMessageBox::AcceptRole | 0 | 单击该按钮将接受对话框(如确定)。 |
QMessageBox::RejectRole | 1 | 单击按钮会拒绝对话框(如取消)。 |
QMessageBox::DestructiveRole | 2 | 单击按钮会导致破坏性更改(例如,丢弃更改)并关闭对话框。 |
QMessageBox::ActionRole | 3 | 单击按钮会更改对话框中的元素。 |
QMessageBox::HelpRole | 4 | 单击该按钮可以请求帮助。 |
QMessageBox::YesRole | 5 | 该按钮类似于 "是 "按钮。 |
QMessageBox::NoRole | 6 | 该按钮类似于 "否 "按钮。 |
QMessageBox::ApplyRole | 8 | 该按钮可应用当前更改。 |
QMessageBox::ResetRole | 7 | 按钮将对话框的字段重置为默认值。 |
另请参阅 StandardButton 。
enum QMessageBox::Icon
该枚举有以下值:
常量 | 值 | 说明 |
---|---|---|
QMessageBox::NoIcon | 0 | 消息框没有任何图标。 |
QMessageBox::Question | 4 | 图标表示信息是在提问。 |
QMessageBox::Information | 1 | 图标表示该信息并无异常。 |
QMessageBox::Warning | 2 | 图标表示该信息是一个警告,但可以处理。 |
QMessageBox::Critical | 3 | 一个图标,表示该信息是一个关键问题。 |
[since 6.6]
枚举类 QMessageBox::Option
标志 QMessageBox::Options
常量 | 值 | 说明 |
---|---|---|
QMessageBox::Option::DontUseNativeDialog | 0x00000001 | 不使用本地消息对话框。 |
此枚举在 Qt 6.6 中引入。
Options 类型是QFlags<Option> 的类型定义。它存储 Option 值的 OR 组合。
枚举QMessageBox::StandardButton
标志 QMessageBox::StandardButtons
这些枚举描述了标准按钮的标志。每个按钮都有一个定义的ButtonRole 。
常量 | 值 | 说明 |
---|---|---|
QMessageBox::Ok | 0x00000400 | 一个 "确定 "按钮,由AcceptRole. |
QMessageBox::Open | 0x00002000 | 通过AcceptRole 定义的 "打开 "按钮。 |
QMessageBox::Save | 0x00000800 | 通过AcceptRole 定义的 "保存 "按钮。 |
QMessageBox::Cancel | 0x00400000 | 通过RejectRole 定义的 "取消 "按钮。 |
QMessageBox::Close | 0x00200000 | 通过RejectRole 定义的 "关闭 "按钮。 |
QMessageBox::Discard | 0x00800000 | 通过DestructiveRole 定义的 "丢弃 "或 "不保存 "按钮(取决于平台)。 |
QMessageBox::Apply | 0x02000000 | 通过ApplyRole 定义的 "应用 "按钮。 |
QMessageBox::Reset | 0x04000000 | 通过ResetRole 定义的 "重置 "按钮。 |
QMessageBox::RestoreDefaults | 0x08000000 | 通过ResetRole 定义的 "恢复默认值 "按钮。 |
QMessageBox::Help | 0x01000000 | 通过HelpRole 定义的 "帮助 "按钮。 |
QMessageBox::SaveAll | 0x00001000 | 通过AcceptRole 定义的 "全部保存 "按钮。 |
QMessageBox::Yes | 0x00004000 | 通过YesRole 定义的 "是 "按钮。 |
QMessageBox::YesToAll | 0x00008000 | 通过YesRole 定义的 "是全部 "按钮。 |
QMessageBox::No | 0x00010000 | 通过NoRole 定义的 "否 "按钮。 |
QMessageBox::NoToAll | 0x00020000 | 通过NoRole 定义的 "否全部 "按钮。 |
QMessageBox::Abort | 0x00040000 | 通过RejectRole 定义的 "放弃 "按钮。 |
QMessageBox::Retry | 0x00080000 | 通过AcceptRole 定义的 "重试 "按钮。 |
QMessageBox::Ignore | 0x00100000 | 通过AcceptRole 定义的 "忽略 "按钮。 |
QMessageBox::NoButton | 0x00000000 | 无效按钮。 |
以下值已过时:
常量 | 值 | 说明 |
---|---|---|
QMessageBox::YesAll | YesToAll | 使用 YesToAll 代替。 |
QMessageBox::NoAll | NoToAll | 使用 NoToAll 代替。 |
QMessageBox::Default | 0x00000100 | 使用information(),warning() 等的defaultButton 参数,或调用setDefaultButton(). |
QMessageBox::Escape | 0x00000200 | 调用setEscapeButton() 代替。 |
QMessageBox::FlagMask | 0x00000300 | |
QMessageBox::ButtonMask | ~FlagMask |
StandardButtons 类型是QFlags<StandardButton> 的类型定义。它存储了 StandardButton 值的 OR 组合。
另请参阅 ButtonRole 和standardButtons 。
属性文档
detailedText : QString
该属性用于保存要在详细信息区域显示的文本。
文本将被解释为纯文本。
默认情况下,该属性包含一个空字符串。
访问功能:
QString | detailedText() const |
void | setDetailedText(const QString &text) |
另请参阅 QMessageBox::text 和QMessageBox::informativeText 。
icon : Icon
该属性包含消息框的图标
信息框图标可以用其中一个值指定:
- QMessageBox::NoIcon
- QMessageBox::Question
- QMessageBox::Information
- QMessageBox::Warning
- QMessageBox::Critical
默认值为QMessageBox::NoIcon 。
用于显示实际图标的像素图取决于当前的GUI style 。您也可以通过设置icon pixmap 属性为图标设置自定义像素图。
访问功能:
QMessageBox::Icon | icon() const |
void | setIcon(QMessageBox::Icon) |
另请参阅 iconPixmap 。
iconPixmap : QPixmap
该属性保存当前图标
消息框当前使用的图标。请注意,通常很难绘制出适合所有 GUI 风格的像素图;您可能需要为每个平台提供不同的像素图。
默认情况下,此属性未定义。
访问函数:
QPixmap | iconPixmap() const |
void | setIconPixmap(const QPixmap &pixmap) |
另请参阅 icon 。
informativeText : QString
该属性包含对信息进行更全面描述的信息文本。
信息文本可用于扩展text() 的内容,为用户提供更多信息,例如描述情况的后果,或建议其他解决方案。
根据文本格式设置(QMessageBox::textFormat ),文本将被解释为纯文本或富文本。默认设置为Qt::AutoText ,即消息框会尝试自动检测文本格式。
默认情况下,该属性包含一个空字符串。
访问功能:
QString | informativeText() const |
void | setInformativeText(const QString &text) |
另请参阅 textFormat,QMessageBox::text, 和QMessageBox::detailedText 。
[since 6.6]
options : Options
影响对话框外观的选项。
默认情况下,这些选项是禁用的。
应在更改对话框属性或显示对话框之前设置选项Option::DontUseNativeDialog 。
在对话框可见时设置选项不能保证立即对对话框产生影响。
在更改其他属性后设置选项可能会导致这些值无效。
此属性在 Qt 6.6 中引入。
访问功能:
QMessageBox::Options | options() const |
void | setOptions(QMessageBox::Options options) |
另请参阅 setOption() 和testOption()。
standardButtons : StandardButtons
消息框中的标准按钮集合
该属性控制消息框使用哪些标准按钮。
默认情况下,该属性不包含任何标准按钮。
访问功能:
QMessageBox::StandardButtons | standardButtons() const |
void | setStandardButtons(QMessageBox::StandardButtons buttons) |
另请参阅 addButton().
text : QString
该属性用于保存要显示的信息框文本。
文本应是描述情况的简短句子或短语,最好是中性陈述或号召行动的问题。
文本将根据文本格式设置 (QMessageBox::textFormat) 被解释为纯文本或富文本。默认设置为Qt::AutoText ,即消息框会尝试自动检测文本格式。
此属性的默认值为空字符串。
访问功能:
QString | text() const |
void | setText(const QString &text) |
另请参阅 textFormat,QMessageBox::informativeText, 和QMessageBox::detailedText 。
textFormat : Qt::TextFormat
该属性用于保存消息框显示的文本格式
消息框使用的当前文本格式。有关可能选项的说明,请参阅Qt::TextFormat 枚举。
默认格式为Qt::AutoText 。
访问功能:
Qt::TextFormat | textFormat() const |
void | setTextFormat(Qt::TextFormat format) |
另请参阅 setText().
textInteractionFlags : Qt::TextInteractionFlags
指定消息框标签与用户输入的交互方式。
默认值取决于样式。
访问功能:
Qt::TextInteractionFlags | textInteractionFlags() const |
void | setTextInteractionFlags(Qt::TextInteractionFlags flags) |
成员函数文档
[explicit]
QMessageBox::QMessageBox(QWidget *parent = nullptr)
构造一个无文本、无按钮的application modal 消息框。parent 传递给QDialog 构造函数。
窗口模式可在调用show() 之前通过setWindowModality() 重写。
在 macOS 上,如果希望消息框显示为其parent 的Qt::Sheet ,请将消息框的window modality 设置为Qt::WindowModal 或使用open() 。否则,消息框将是一个标准对话框。
另请参阅 setWindowTitle()、setText()、setIcon()、setStandardButtons() 和setWindowModality()。
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)
使用给定的icon,title,text 和标准buttons 构建application modal 消息框。可随时使用addButton() 添加标准或自定义按钮。parent 和f 参数传递给QDialog 构造函数。
窗口模式可在调用show() 之前通过setWindowModality() 进行重写。
在 macOS 上,如果parent 不是nullptr
,且您希望消息框显示为该父对象的Qt::Sheet ,请将消息框的window modality 设置为Qt::WindowModal (默认)。否则,消息框将是一个标准对话框。
另请参阅 setWindowTitle(),setText(),setIcon(),setStandardButtons() 和setWindowModality().
[virtual noexcept]
QMessageBox::~QMessageBox()
销毁消息框。
[static]
void QMessageBox::about(QWidget *parent, const QString &title, const QString &text)
显示一个简单的关于框,标题为title ,文本为text 。关于框的父级是parent 。
about() 会在四个位置查找合适的图标:
- 如果存在,它会首选parent->icon() 。
- 如果不存在,它会尝试包含parent 的顶层 widget。
- 如果失败,它会尝试active window.
- 最后,它会使用 "信息 "图标。
关于框有一个标有 "确定 "的按钮。
在 macOS 上,"关于 "框以无模式窗口的形式弹出;而在其他平台上,它目前是应用程序模式。
另请参阅 QWidget::windowIcon() 和QApplication::activeWindow()。
[static]
void QMessageBox::aboutQt(QWidget *parent, const QString &title = QString())
显示一个有关 Qt 的简单消息框,内容为给定的title ,并以parent 为中心(如果parent 不是nullptr
)。信息包括应用程序使用的 Qt 版本号。
如菜单示例所示,这对于将其纳入应用程序的Help 菜单非常有用。
QApplication 在 MacOS 上,aboutQt 作为槽提供了这一功能。
在 macOS 上,aboutQt 框以无模式窗口的形式弹出;而在其他平台上,它目前是应用程序模式。
另请参见 QApplication::aboutQt()。
void QMessageBox::addButton(QAbstractButton *button, QMessageBox::ButtonRole role)
将给定的button 与指定的role 一起添加到消息框中。
另请参阅 removeButton()、button() 和setStandardButtons()。
QPushButton *QMessageBox::addButton(QMessageBox::StandardButton button)
这是一个重载函数。
如果有效,则在消息框中添加标准button ,并返回按钮。
另请参阅 setStandardButtons() 。
QPushButton *QMessageBox::addButton(const QString &text, QMessageBox::ButtonRole role)
这是一个重载函数。
使用给定的text 创建一个按钮,并将其添加到指定role 的消息框中,然后返回。
QAbstractButton *QMessageBox::button(QMessageBox::StandardButton which) const
返回与标准按钮which 相对应的指针,如果此消息框中不存在标准按钮,则返回nullptr
。
注意: 修改返回按钮的属性可能不会反映在消息对话框的本地实现中。要自定义对话框按钮,请添加custom button 或button title ,或设置Option::DontUseNativeDialog 选项。
另请参阅 standardButtons 和standardButton()。
[signal]
void QMessageBox::buttonClicked(QAbstractButton *button)
每当QMessageBox 中的按钮被点击时,就会发出该信号。被点击的按钮会在button 中返回。
QMessageBox::ButtonRole QMessageBox::buttonRole(QAbstractButton *button) const
返回指定button 的按钮角色。如果button 是nullptr
或尚未添加到消息框,则此函数返回InvalidRole 。
QList<QAbstractButton *> QMessageBox::buttons() const
返回已添加到消息框中的所有按钮的列表。
另请参阅 buttonRole()、addButton() 和removeButton()。
[override virtual protected]
void QMessageBox::changeEvent(QEvent *ev)
重实现:QWidget::changeEvent(QEvent *event).
QCheckBox *QMessageBox::checkBox() const
返回对话框上显示的复选框。如果未设置复选框,则返回nullptr
。
另请参阅 setCheckBox()。
QAbstractButton *QMessageBox::clickedButton() const
返回用户点击的按钮,如果用户点击了Esc 键且未设置escape button ,则返回nullptr
。
如果exec() 尚未被调用,则返回 nullptr。
示例
QMessageBox messageBox(this); QAbstractButton *disconnectButton = messageBox.addButton(tr("Disconnect"), QMessageBox::ActionRole); ... messageBox.exec(); if (messageBox.clickedButton() == disconnectButton) { ... }
另请参阅 standardButton() 和button()。
[override virtual protected]
void QMessageBox::closeEvent(QCloseEvent *e)
重实现:QDialog::closeEvent(QCloseEvent *e)。
[static]
QMessageBox::StandardButton QMessageBox::critical(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)
在指定的parent widget 前面用给定的title 和text 打开一个关键消息框。
buttons defaultButton 指定按下 时使用的按钮。 必须是 中给定的按钮。如果 是 , 会自动选择一个合适的默认按钮。Enter defaultButton buttons defaultButton QMessageBox::NoButton QMessageBox
返回被点击的标准按钮的标识。如果Esc 被按下,则返回escape button 。
消息框是application modal 对话框。
警告: 请勿在执行对话框期间删除parent 。如果要删除,应使用QMessageBox 构造函数自行创建对话框。
另请参阅 question()、warning() 和information()。
QPushButton *QMessageBox::defaultButton() const
返回信息框default button 的按钮。如果未设置默认按钮,则返回 nullptr。
另请参阅 setDefaultButton()、addButton() 和QPushButton::setDefault()。
QAbstractButton *QMessageBox::escapeButton() const
返回按下 Escape 键时激活的按钮。
默认情况下,QMessageBox 会按以下方式自动检测逃逸按钮:
- 如果只有一个按钮,则将其作为逃逸按钮。
- 如果有一个Cancel 按钮,则将其作为 Escape 按钮。
- 仅在 macOS 上,如果正好有一个角色为QMessageBox::RejectRole 的按钮,则将其作为转义按钮。
如果无法自动检测到逃逸按钮,则按Esc 没有任何作用。
另请参阅 setEscapeButton() 和addButton()。
[override virtual protected]
bool QMessageBox::event(QEvent *e)
重实现:QWidget::event(QEvent *event).
[override virtual slot]
int QMessageBox::exec()
重新实现:QDialog::exec().
将消息框显示为modal dialog ,阻塞直到用户关闭。
当QMessageBox 与标准按钮一起使用时,该函数会返回一个StandardButton 值,指示被点击的标准按钮。当QMessageBox 与自定义按钮一起使用时,该函数将返回一个不透明值;请使用clickedButton() 来确定哪个按钮被点击。
注意: result() 函数也返回StandardButton 值,而不是QDialog::DialogCode 。
在点击按钮或使用窗口系统提供的机制关闭对话框之前,用户无法与同一应用程序中的任何其他窗口交互。
[static]
QMessageBox::StandardButton QMessageBox::information(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)
在指定的parent widget 前面用给定的title 和text 打开一个信息消息框。
buttons defaultButton 指定按下 时使用的按钮。 必须是 中给定的按钮。如果 是 , 会自动选择一个合适的默认按钮。Enter defaultButton buttons defaultButton QMessageBox::NoButton QMessageBox
返回被点击的标准按钮的标识。如果Esc 被按下,则返回escape button 。
消息框是application modal 对话框。
警告: 请勿在执行对话框期间删除parent 。如果要删除,应使用QMessageBox 构造函数自行创建对话框。
另请参阅 question()、warning() 和critical()。
[override virtual protected]
void QMessageBox::keyPressEvent(QKeyEvent *e)
重实现:QDialog::keyPressEvent(QKeyEvent *e)。
void QMessageBox::open(QObject *receiver, const char *member)
打开对话框,并将其finished() 或buttonClicked() 信号连接到receiver 和member 指定的槽。如果member 中的槽的第一个参数是指针,则连接到buttonClicked(),否则连接到finished()。
关闭对话框时,信号将从插槽中断开。
[static]
QMessageBox::StandardButton QMessageBox::question(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = StandardButtons(Yes | No), QMessageBox::StandardButton defaultButton = NoButton)
在指定的parent widget 前面用给定的title 和text 打开一个问题消息框。
buttons defaultButton 指定按下 时使用的按钮。 必须是 中给定的按钮。如果 是 , 会自动选择一个合适的默认按钮。Enter defaultButton buttons defaultButton QMessageBox::NoButton QMessageBox
返回被点击的标准按钮的标识。如果Esc 被按下,则返回escape button 。
消息框是application modal 对话框。
警告: 请勿在执行对话框期间删除parent 。如果要删除,应使用QMessageBox 构造函数自行创建对话框。
另请参阅 information()、warning() 和critical()。
void QMessageBox::removeButton(QAbstractButton *button)
从按钮框中删除button ,但不删除它。
另请参阅 addButton() 和setStandardButtons()。
[override virtual protected]
void QMessageBox::resizeEvent(QResizeEvent *event)
重实现:QDialog::resizeEvent(QResizeEvent *)。
void QMessageBox::setCheckBox(QCheckBox *cb)
设置消息对话框cb 中的复选框。消息框拥有复选框的所有权。参数cb 可用于nullptr
从消息框中删除现有复选框。
另请参阅 checkBox()。
void QMessageBox::setDefaultButton(QMessageBox::StandardButton button)
将消息框的default button 设置为button 。
另请参阅 defaultButton()、addButton() 和QPushButton::setDefault()。
void QMessageBox::setDefaultButton(QPushButton *button)
将消息框的default button 设置为button 。
另请参阅 addButton() 和QPushButton::setDefault()。
void QMessageBox::setEscapeButton(QAbstractButton *button)
将Escape 键按下时激活的按钮设置为button 。
另请参阅 escapeButton()、addButton() 和clickedButton()。
void QMessageBox::setEscapeButton(QMessageBox::StandardButton button)
将按Escape 键时激活的按钮设置为button 。
另请参阅 addButton() 和clickedButton()。
[since 6.6]
void QMessageBox::setOption(QMessageBox::Option option, bool on = true)
如果on 为 true,则设置option 为启用状态;否则,清除给定的option 。
应在显示对话框之前设置选项(尤其是Option::DontUseNativeDialog 选项)。
在对话框可见时设置选项并不能保证立即对对话框产生影响。
在更改其他属性后设置选项可能会导致这些值无效。
此函数在 Qt 6.6 中引入。
另请参阅 options 和testOption()。
void QMessageBox::setWindowModality(Qt::WindowModality windowModality)
该函数阴影QWidget::setWindowModality().
将消息框的模式设置为windowModality 。
在 macOS 上,如果模式设置为Qt::WindowModal 且消息框有父对象,则消息框将是Qt::Sheet ,否则消息框将是标准对话框。
void QMessageBox::setWindowTitle(const QString &title)
该函数阴影QWidget::setWindowTitle().
将消息框的标题设置为title 。在 macOS 上,窗口标题将被忽略(根据 macOS 指南的要求)。
[override virtual protected]
void QMessageBox::showEvent(QShowEvent *e)
重实现:QDialog::showEvent(QShowEvent *event).
QMessageBox::StandardButton QMessageBox::standardButton(QAbstractButton *button) const
返回与给定的button 对应的标准按钮枚举值,如果给定的button 不是标准按钮,则返回NoButton 。
另请参阅 button() 和standardButtons()。
[since 6.6]
bool QMessageBox::testOption(QMessageBox::Option option) const
如果给定的option 已启用,则返回true
;否则返回 false。
此函数在 Qt 6.6 中引入。
[static]
QMessageBox::StandardButton QMessageBox::warning(QWidget *parent, const QString &title, const QString &text, QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)
在指定的parent widget 前面用给定的title 和text 打开一个警告消息框。
标准buttons 会添加到消息框中。defaultButton 指定Enter 被按下时使用的按钮。defaultButton 必须是buttons 中给定的按钮。如果defaultButton 是QMessageBox::NoButton ,QMessageBox 会自动选择一个合适的默认按钮。
返回被点击的标准按钮的标识。如果Esc 被按下,则返回escape button 。
消息框是application modal 对话框。
警告: 请勿在执行对话框期间删除parent 。如果要删除,应使用QMessageBox 构造函数自行创建对话框。
另请参阅 question()、information() 和critical()。
宏文档
QT_REQUIRE_VERSION(int argc, char **argv, const char *version)
该宏可用于确保应用程序在足够新的 Qt 版本下运行。如果您的应用程序依赖于某个错误修复版本(如 6.1.2)中引入的特定错误修复,该宏尤其有用。
argc 和argv 参数是main()
函数的argc
和argv
参数。version 参数是一个字符串,用于指定应用程序所需的 Qt 版本(例如 "6.1.2")。
示例
#include <QApplication> #include <QMessageBox> int main(int argc, char *argv[]) { QT_REQUIRE_VERSION(argc, argv, "4.0.2") QApplication app(argc, argv); ... return app.exec(); }
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.