QMessageBox 类

QMessageBox 类提供用于告知用户或向用户询问问题并接收答案的模态对话框。 更多...

头: #include <QMessageBox>
CMake: find_package(Qt6 REQUIRED COMPONENTS Widgets)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QDialog

公共类型

enum ButtonRole { InvalidRole, AcceptRole, RejectRole, DestructiveRole, ActionRole, …, ResetRole }
enum 图标 { NoIcon, Question, Information, Warning, Critical }
(从 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 (const QString & text , QMessageBox::ButtonRole role )
QPushButton * addButton (QMessageBox::StandardButton button )
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 (QPushButton * button )
void setDefaultButton (QMessageBox::StandardButton 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 )
(从 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
(从 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 以向用户发出状况警报, 情报文本 to further explain the situation, and an optional 细节文本 to provide even more data if the user requests it.

A message box can also display an icon and 标准按钮 为接受用户响应。

为使用 QMessageBox 提供了 2 个 API:基于特性的 API 和静态函数。调用某一静态函数方式更简单,但它不如使用基于特性的 API 灵活,且结果情报较少。推荐使用基于属性的 API。

基于特性的 API

To use the property-based API, construct an instance of QMessageBox, set the desired properties, and call exec () to show the message. The simplest configuration is to set only the 消息文本 特性。

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

用户必须点击 OK button to dismiss the message box. The rest of the GUI is blocked until the message box is dismissed.

A better approach than just alerting the user to an event is to also ask the user what to do about it.

设置 标准按钮 property to the set of buttons you want as the set of user responses. The buttons are specified by combining values from StandardButtons using the bitwise OR operator. The display order for the buttons is platform-dependent. For example, on Windows, Save is displayed to the left of Cancel , whereas on macOS, the order is reversed. Mark one of your standard buttons to be your 默认按钮 .

The 情报文本 property can be used to add additional context to help the user choose the appropriate action.

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();
					

The exec () 槽返回 StandardButtons value of the button that was clicked.

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;
}
					

To give the user more information to help them choose the appropriate, action, set the 细节文本 property. Depending on the platform the 细节文本 , may require the user to click a Show Details... button to be shown.

点击 Show Details... button displays the detailed text.

富文本和文本格式特性

The 细节文本 property is always interpreted as plain text. The main text and 情报文本 properties can be either plain text or rich text. These strings are interpreted according to the setting of the 文本格式 property. The default setting is auto-text .

Note that for some plain text strings containing XML meta-characters, the auto-text rich text detection test may fail causing your plain text string to be interpreted incorrectly as rich text. In these rare cases, use Qt::convertFromPlainText () to convert your plain text string to a visually equivalent rich text string, or set the 文本格式 property explicitly with setTextFormat ().

严重级别、图标和像素图特性

QMessageBox supports four predefined message severity levels, or message types, which really only differ in the predefined icon they each show. Specify one of the four predefined message types by setting the icon property to one of the 预定义图标 . The following rules are guidelines:

询问 For asking a question during normal operations.
信息 For reporting information about normal operations.
警告 For reporting non-critical errors.
危急 For reporting critical errors.

预定义图标 are not defined by QMessageBox, but provided by the style. The default value is No Icon . The message boxes are otherwise the same for all cases. When using a standard icon, use the one recommended in the table, or use the one recommended by the style guidelines for your platform. If none of the standard icons is right for your message box, you can use a custom icon by setting the icon pixmap property instead of setting the icon 特性。

In summary, to set an icon, use either setIcon () for one of the standard icons, or setIconPixmap () for a custom icon.

静态函数 API

Building message boxes with the static functions API, although convenient, is less flexible than using the property-based API, because the static function signatures lack parameters for setting the 情报文本 and 细节文本 properties. One work-around for this has been to use the title parameter as the message box main text and the text parameter as the message box informative text. Because this has the obvious drawback of making a less readable message box, platform guidelines do not recommend it. The Microsoft Windows User Interface Guidelines recommend using the 应用程序名称 作为 窗口标题 , which means that if you have an informative text in addition to your main text, you must concatenate it to the text 参数。

Note that the static function signatures have changed with respect to their button parameters, which are now used to set the 标准按钮 默认按钮 .

Static functions are available for creating information (), question (), warning (),和 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);
					

The 标准对话框 example shows how to use QMessageBox and the other built-in Qt dialogs.

高级用法

标准按钮 are not flexible enough for your message box, you can use the addButton () overload that takes a text and a ButtonRole to add custom buttons. The ButtonRole is used by QMessageBox to determine the ordering of the buttons on screen (which varies according to the platform). You can test the value of clickedButton () after calling exec (). For example,

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
}
					

默认键和 Esc 键

The default button (i.e., the button activated when Enter is pressed) can be specified using setDefaultButton (). If a default button is not specified, QMessageBox tries to find one based on the button roles of the buttons used in the message box.

The escape button (the button activated when Esc is pressed) can be specified using setEscapeButton (). If an escape button is not specified, QMessageBox tries to find one using these rules:

  1. If there is only one button, it is the button activated when Esc is pressed.
  2. 若有 Cancel button, it is the button activated when Esc is pressed.
  3. If there is exactly one button having either the Reject role the No role , it is the button activated when Esc is pressed.

When an escape button can't be determined using these rules, pressing Esc 不起作用。

另请参阅 QDialogButtonBox and 标准对话框范例 .

成员类型文档编制

enum QMessageBox:: ButtonRole

This enum describes the roles that can be used to describe buttons in the button box. Combinations of these roles are as flags used to describe different aspects of their behavior.

常量 描述
QMessageBox::InvalidRole -1 按钮无效。
QMessageBox::AcceptRole 0 Clicking the button causes the dialog to be accepted (e.g. OK).
QMessageBox::RejectRole 1 Clicking the button causes the dialog to be rejected (e.g. Cancel).
QMessageBox::DestructiveRole 2 Clicking the button causes a destructive change (e.g. for Discarding Changes) and closes the dialog.
QMessageBox::ActionRole 3 点击按钮导致对话框中元素改变。
QMessageBox::HelpRole 4 可以点击按钮以请求帮助。
QMessageBox::YesRole 5 The button is a "Yes"-like button.
QMessageBox::NoRole 6 The button is a "No"-like button.
QMessageBox::ApplyRole 8 The button applies current changes.
QMessageBox::ResetRole 7 The button resets the dialog's fields to default values.

另请参阅 StandardButton .

enum QMessageBox:: 图标

This enum has the following values:

常量 描述
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.

[since 6.6] enum class QMessageBox:: Option
flags QMessageBox:: Options

常量 描述
QMessageBox::Option::DontUseNativeDialog 0x00000001 Don't use the native message dialog.

该枚举在 Qt 6.6 引入。

Options 类型是 typedef 对于 QFlags <Option>。它存储 Option 值的 OR 组合。

enum QMessageBox:: StandardButton
flags QMessageBox:: StandardButtons

These enums describe flags for standard buttons. Each button has a defined ButtonRole .

常量 描述
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 无效按钮。

以下值已过时:

常量 描述
QMessageBox::YesAll YesToAll 使用 YesToAll 代替。
QMessageBox::NoAll NoToAll 使用 NoToAll 代替。
QMessageBox::Default 0x00000100 使用 defaultButton argument of information (), warning (), etc. instead, or call setDefaultButton ().
QMessageBox::Escape 0x00000200 调用 setEscapeButton () 代替。
QMessageBox::FlagMask 0x00000300
QMessageBox::ButtonMask ~FlagMask

StandardButtons 类型是 typedef 对于 QFlags <StandardButton>. It stores an OR combination of StandardButton values.

另请参阅 ButtonRole and standardButtons .

特性文档编制

detailedText : QString

This property holds the text to be displayed in the details area.

The text will be interpreted as a plain text.

默认情况下,此特性包含空字符串。

访问函数:

QString detailedText () const
void setDetailedText (const QString & text )

另请参阅 QMessageBox::text and QMessageBox::informativeText .

icon : 图标

此特性保持消息框的图标

The icon of the message box can be specified with one of the values:

默认为 QMessageBox::NoIcon .

The pixmap used to display the actual icon depends on the current GUI style . You can also set a custom pixmap for the icon by setting the icon pixmap 特性。

访问函数:

QMessageBox::Icon icon () const
void setIcon (QMessageBox::Icon)

另请参阅 iconPixmap .

iconPixmap : QPixmap

此特性保持当前图标

The icon currently used by the message box. Note that it's often hard to draw one pixmap that looks appropriate in all GUI styles; you may want to supply a different pixmap for each platform.

By default, this property is undefined.

访问函数:

QPixmap iconPixmap () const
void setIconPixmap (const QPixmap & pixmap )

另请参阅 icon .

informativeText : QString

This property holds the informative text that provides a fuller description for the message

Informative text can be used to expand upon the text () to give more information to the user, for example describing the consequences of the situation, or suggestion alternative solutions.

默认情况下,此特性包含空字符串。

访问函数:

QString informativeText () const
void setInformativeText (const QString & text )

另请参阅 QMessageBox::text and QMessageBox::detailedText .

[since 6.6] options : Options

Options that affect the look and feel of the dialog.

By default, these options are disabled.

The option Option::DontUseNativeDialog should be set before changing dialog properties or showing the dialog.

Setting options while the dialog is visible is not guaranteed to have an immediate effect on the dialog.

设置选项在改变其它特性后,可能导致这些值不起作用。

该特性在 Qt 6.6 引入。

访问函数:

QMessageBox::Options options () const
void setOptions (QMessageBox::Options options )

另请参阅 setOption () 和 testOption ().

standardButtons : StandardButtons

消息框中的标准按钮集合

This property controls which standard buttons are used by the message box.

默认情况下,此特性不包含标准按钮。

访问函数:

QMessageBox::StandardButtons standardButtons () const
void setStandardButtons (QMessageBox::StandardButtons buttons )

另请参阅 addButton ().

text : QString

This property holds the message box text to be displayed.

The text should be a brief sentence or phrase that describes the situation, ideally formulated as a neutral statement, or a call-to-action question.

The text will be interpreted either as a plain text or as rich text, depending on the text format setting ( QMessageBox::textFormat ). The default setting is Qt::AutoText , i.e., the message box will try to auto-detect the format of the text.

The default value of this property is an empty string.

访问函数:

QString text () const
void setText (const QString & text )

另请参阅 textFormat , QMessageBox::informativeText ,和 QMessageBox::detailedText .

textFormat : Qt::TextFormat

This property holds the format of the text displayed by the message box

The current text format used by the message box. See the Qt::TextFormat 枚举了解可能选项的解释。

默认格式为 Qt::AutoText .

访问函数:

Qt::TextFormat textFormat () const
void setTextFormat (Qt::TextFormat format )

另请参阅 setText ().

textInteractionFlags : Qt::TextInteractionFlags

Specifies how the label of the message box should interact with user input.

默认值取决于样式。

访问函数:

Qt::TextInteractionFlags textInteractionFlags () const
void setTextInteractionFlags (Qt::TextInteractionFlags flags )

另请参阅 QStyle::SH_MessageBox_TextInteractionFlags .

成员函数文档编制

[explicit] QMessageBox:: QMessageBox ( QWidget * parent = nullptr)

Constructs an 应用程序模态 message box with no text and no buttons. parent 被传递给 QDialog 构造函数。

The window modality can be overridden via setWindowModality () 先于调用 show ().

注意: 使用 open () 或 exec () to show the message box affects the window modality. Please see the detailed documentation for each function for more information.

On macOS, if you want your message box to appear as a Qt::Sheet of its parent , set the message box's window modality to Qt::WindowModal or use open (). Otherwise, the message box will be a standard dialog.

另请参阅 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)

Constructs an 应用程序模态 message box with the given icon , title , text , and standard buttons . Standard or custom buttons can be added at any time using addButton ()。 parent and f 自变量被传递给 QDialog 构造函数。

The window modality can be overridden via setWindowModality () 先于调用 show ().

注意: 使用 open () 或 exec () to show the message box affects the window modality. Please see the detailed documentation for each function for more information.

在 macOS,若 parent 不是 nullptr and you want your message box to appear as a Qt::Sheet of that parent, set the message box's window modality to Qt::WindowModal (default). Otherwise, the message box will be a standard dialog.

另请参阅 setWindowTitle (), setText (), setIcon (), setStandardButtons (),和 setWindowModality ().

[virtual noexcept] QMessageBox:: ~QMessageBox ()

销毁消息框。

[static] void QMessageBox:: about ( QWidget * parent , const QString & title , const QString & text )

Displays a simple about box with title title 和文本 text . The about box's parent is parent .

about() looks for a suitable icon in four locations:

  1. It prefers parent->icon () if that exists.
  2. If not, it tries the top-level widget containing parent .
  3. If that fails, it tries the active window.
  4. As a last resort it uses the Information icon.

The about box has a single button labelled "OK".

On macOS, the about box is popped up as a modeless window; on other platforms, it is currently application modal.

另请参阅 QWidget::windowIcon () 和 QApplication::activeWindow ().

[static] void QMessageBox:: aboutQt ( QWidget * parent , const QString & title = QString())

Displays a simple message box about Qt, with the given title and centered over parent (if parent 不是 nullptr ). The message includes the version number of Qt being used by the application.

这很有用,对于包括 帮助 菜单的应用程序,如展示在 菜单 范例。

QApplication provides this functionality as a slot.

On macOS, the aboutQt box is popped up as a modeless window; on other platforms, it is currently application modal.

另请参阅 QApplication::aboutQt ().

void QMessageBox:: addButton ( QAbstractButton * button , QMessageBox::ButtonRole role )

添加给定 button to the message box with the specified role .

另请参阅 removeButton (), button (),和 setStandardButtons ().

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

这是重载函数。

Creates a button with the given text , adds it to the message box for the specified role , and returns it.

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

这是重载函数。

添加标准 button to the message box if it is valid to do so, and returns the push button.

另请参阅 setStandardButtons ().

QAbstractButton *QMessageBox:: button ( QMessageBox::StandardButton which ) const

Returns a pointer corresponding to the standard button which ,或 nullptr if the standard button doesn't exist in this message box.

注意: Modifying the properties of the returned button may not be reflected in native implementations of the message dialog. To customize dialog buttons add a custom button or button title instead, or set the Option::DontUseNativeDialog 选项。

另请参阅 standardButtons and standardButton ().

[signal] void QMessageBox:: buttonClicked ( QAbstractButton * button )

This signal is emitted whenever a button is clicked inside the QMessageBox . The button that was clicked in returned in button .

QMessageBox::ButtonRole QMessageBox:: buttonRole ( QAbstractButton * button ) const

Returns the button role for the specified button 。此函数返回 InvalidRole if button is nullptr or has not been added to the message box.

另请参阅 buttons () 和 addButton ().

QList < QAbstractButton *> QMessageBox:: buttons () const

Returns a list of all the buttons that have been added to the message box.

另请参阅 buttonRole (), addButton (),和 removeButton ().

[override virtual protected] void QMessageBox:: changeEvent ( QEvent * ev )

重实现: QWidget::changeEvent (QEvent *event).

QCheckBox *QMessageBox:: checkBox () const

Returns the checkbox shown on the dialog. This is nullptr if no checkbox is set.

另请参阅 setCheckBox ().

QAbstractButton *QMessageBox:: clickedButton () const

Returns the button that was clicked by the user, or nullptr if the user hit the Esc key and no Esc 按钮 was set.

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)

Opens a critical message box with the given title and text in front of the specified parent 小部件。

标准 buttons 被添加到消息框。 defaultButton specifies the button used when Enter is pressed. defaultButton must refer to a button that was given in buttons 。若 defaultButton is QMessageBox::NoButton , QMessageBox chooses a suitable default automatically.

Returns the identity of the standard button that was clicked. If Esc was pressed instead, the Esc 按钮 被返回。

消息框是 应用程序模态 对话框。

警告: 不要删除 parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QMessageBox 构造函数。

另请参阅 question (), warning (),和 information ().

QPushButton *QMessageBox:: defaultButton () const

Returns the button that should be the message box's 默认按钮 。返回 nullptr 若未设置默认按钮。

另请参阅 setDefaultButton (), addButton (),和 QPushButton::setDefault ().

QAbstractButton *QMessageBox:: escapeButton () const

返回被激活按钮,当按下 Esc 键时。

默认情况下, QMessageBox attempts to automatically detect an escape button as follows:

  1. If there is only one button, it is made the escape button.
  2. 若有 Cancel button, it is made the escape button.
  3. On macOS only, if there is exactly one button with the role QMessageBox::RejectRole , it is made the escape button.

When an escape button could not be automatically detected, pressing Esc 不起作用。

另请参阅 setEscapeButton () 和 addButton ().

[override virtual protected] bool QMessageBox:: event ( QEvent * e )

重实现: QWidget::event (QEvent *event).

[override virtual slot] int QMessageBox:: exec ()

重实现: QDialog::exec ().

展示消息框作为 模态对话框 ,阻塞直到用户关闭它。

当使用 QMessageBox with standard buttons, this function returns a StandardButton value indicating the standard button that was clicked. When using QMessageBox with custom buttons, this function returns an opaque value; use clickedButton () to determine which button was clicked.

注意: The result () function returns also StandardButton value instead of QDialog::DialogCode .

Users cannot interact with any other window in the same application until they close the dialog, either by clicking a button or by using a mechanism provided by the window system.

另请参阅 show () 和 result ().

[static] QMessageBox::StandardButton QMessageBox:: information ( QWidget * parent , const QString & title , const QString & text , QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)

打开信息消息框采用给定 title and text in front of the specified parent 小部件。

标准 buttons 被添加到消息框。 defaultButton specifies the button used when Enter is pressed. defaultButton must refer to a button that was given in buttons 。若 defaultButton is QMessageBox::NoButton , QMessageBox chooses a suitable default automatically.

Returns the identity of the standard button that was clicked. If Esc was pressed instead, the Esc 按钮 被返回。

消息框是 应用程序模态 对话框。

警告: 不要删除 parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the 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 and member 。若槽在 member has a pointer for its first parameter the connection is to buttonClicked (), otherwise the connection is to finished ().

将从槽断开信号连接,当关闭对话框时。

[static] QMessageBox::StandardButton QMessageBox:: question ( QWidget * parent , const QString & title , const QString & text , QMessageBox::StandardButtons buttons = StandardButtons(Yes | No), QMessageBox::StandardButton defaultButton = NoButton)

打开问题消息框采用给定 title and text in front of the specified parent 小部件。

标准 buttons 被添加到消息框。 defaultButton specifies the button used when Enter is pressed. defaultButton must refer to a button that was given in buttons 。若 defaultButton is QMessageBox::NoButton , QMessageBox chooses a suitable default automatically.

Returns the identity of the standard button that was clicked. If Esc was pressed instead, the Esc 按钮 被返回。

消息框是 应用程序模态 对话框。

警告: 不要删除 parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the 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 )

Sets the checkbox cb on the message dialog. The message box takes ownership of the checkbox. The argument cb 可以是 nullptr to remove an existing checkbox from the message box.

另请参阅 checkBox ().

void QMessageBox:: setDefaultButton ( QPushButton * button )

设置消息框的 默认按钮 to button .

另请参阅 defaultButton (), addButton (),和 QPushButton::setDefault ().

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

设置消息框的 默认按钮 to button .

另请参阅 addButton () 和 QPushButton::setDefault ().

void QMessageBox:: setEscapeButton ( QAbstractButton * button )

Sets the button that gets activated when the Escape key is pressed to button .

另请参阅 escapeButton (), addButton (),和 clickedButton ().

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

Sets the buttons that gets activated when the Escape key is pressed to button .

另请参阅 addButton () 和 clickedButton ().

[since 6.6] void QMessageBox:: setOption ( QMessageBox::Option option , bool on = true)

设置给定 option 为被启用若 on 为 true;否则,清零给定 option .

选项 (尤其 Option::DontUseNativeDialog option) should be set before showing the dialog.

Setting options while the dialog is visible is not guaranteed to have an immediate effect on the dialog.

设置选项在改变其它特性后,可能导致这些值不起作用。

该函数在 Qt 6.6 引入。

另请参阅 options and testOption ().

void QMessageBox:: setWindowModality ( Qt::WindowModality windowModality )

此函数投影 QWidget::setWindowModality ().

Sets the modality of the message box to windowModality .

On macOS, if the modality is set to Qt::WindowModal and the message box has a parent, then the message box will be a Qt::Sheet ,否则消息框将是标准对话框。

void QMessageBox:: setWindowTitle (const QString & title )

此函数投影 QWidget::setWindowTitle ().

Sets the title of the message box to title . On macOS, the window title is ignored (as required by the macOS Guidelines).

[override virtual protected] void QMessageBox:: showEvent ( QShowEvent * e )

重实现: QDialog::showEvent (QShowEvent *event).

QMessageBox::StandardButton QMessageBox:: standardButton ( QAbstractButton * button ) const

返回的标准按钮枚举值对应给定 button ,或 NoButton 若给定 button 不是标准按钮。

另请参阅 button () 和 standardButtons ().

[since 6.6] bool QMessageBox:: testOption ( QMessageBox::Option option ) const

返回 true 若给定 option 被启用;否则,返回 false。

该函数在 Qt 6.6 引入。

另请参阅 options and setOption ().

[static] QMessageBox::StandardButton QMessageBox:: warning ( QWidget * parent , const QString & title , const QString & text , QMessageBox::StandardButtons buttons = Ok, QMessageBox::StandardButton defaultButton = NoButton)

Opens a warning message box with the given title and text in front of the specified parent 小部件。

标准 buttons 被添加到消息框。 defaultButton specifies the button used when Enter is pressed. defaultButton must refer to a button that was given in buttons 。若 defaultButton is QMessageBox::NoButton , QMessageBox chooses a suitable default automatically.

Returns the identity of the standard button that was clicked. If Esc was pressed instead, the Esc 按钮 被返回。

消息框是 应用程序模态 对话框。

警告: 不要删除 parent during the execution of the dialog. If you want to do this, you should create the dialog yourself using one of the QMessageBox 构造函数。

另请参阅 question (), information (),和 critical ().

宏文档编制

QT_REQUIRE_VERSION ( int argc , char ** argv , const char * version )

This macro can be used to ensure that the application is run with a recent enough version of Qt. This is especially useful if your application depends on a specific bug fix introduced in a bug-fix release (for example, 6.1.2).

The argc and argv parameters are the main() function's argc and argv parameters. The version parameter is a string literal that specifies which version of Qt the application requires (for example, "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();
}