6.0.0-rc4Source

QMessageBox Class

The QMessageBox class shows a message and asks the user to answer with a button. More…

Header#include <QMessageBox>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQDialog

List of all members, including inherited members

Public Types

Public Functions

QMessageBox(QWidget *parent = nullptr)
QMessageBox(Icon icon, const QString &title, const QString &text, StandardButtons buttons = NoButton, QWidget *parent = nullptr, Qt::WindowFlags f = Qt::Dialog)
~QMessageBox()
voidaddButton(QAbstractButton *button, ButtonRole role)
QPushButton *addButton(const QString &text, ButtonRole role)
QPushButton *addButton(StandardButton button)
voidremoveButton(QAbstractButton *button)
QList<QAbstractButton *>buttons() const
ButtonRolebuttonRole(QAbstractButton *button) const
voidsetStandardButtons(StandardButtons buttons)
StandardButtonsstandardButtons() const
StandardButtonstandardButton(QAbstractButton *button) const
QAbstractButton *button(StandardButton which) const
QPushButton *defaultButton() const
voidsetDefaultButton(QPushButton *button)
voidsetDefaultButton(StandardButton button)
QAbstractButton *escapeButton() const
voidsetEscapeButton(QAbstractButton *button)
voidsetEscapeButton(StandardButton button)
QAbstractButton *clickedButton() const
QStringtext() const
voidsetText(const QString &text)
Iconicon() const
voidsetIcon(Icon icon)
QStringinformativeText() const
voidsetInformativeText(const QString &text)
QStringdetailedText() const
voidsetDetailedText(const QString &text)
voidsetCheckBox(QCheckBox *cb)
QCheckBox *checkBox() const

Public Slots

intexec()

Signals

Static Public Members

StandardButtoninformation(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton)
StandardButtonquestion(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = StandardButtons(Yes) | No, StandardButton defaultButton = NoButton)
StandardButtonwarning(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton)
StandardButtoncritical(QWidget *parent, const QString &title, const QString &text, StandardButtons buttons = Ok, StandardButton defaultButton = NoButton)
voidabout(QWidget *parent, const QString &title, const QString &text)

Reimplemented Protected Functions

voidkeyPressEvent(QKeyEvent *event)
voidcloseEvent(QCloseEvent *event)
voidshowEvent(QShowEvent *event)

Detailed Description

A message box has an icon, a text, and buttons. The static functions show a box and wait for the answer:

if (QMessageBox::question(window, "Quit", "Quit without saving?") == QMessageBox::Yes)
    qApp->quit();

For more, set up a message box and call exec(). It returns the standard button that was clicked:

QMessageBox box(QMessageBox::Warning, "Save", "The document has been modified.",
                QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, window);
box.setInformativeText("Do you want to save your changes?");
box.setDefaultButton(QMessageBox::Save);
switch (box.exec()) {
case QMessageBox::Save: save(); break;
case QMessageBox::Discard: break;
default: return;
}

The buttons are laid out by a QDialogButtonBox. Escape and the close button of the window click the escape button: the one set with setEscapeButton(), or else the button with the RejectRole, or the only button.

See also QDialog and QDialogButtonBox.

Differences from Qt

The icons are drawn rather than taken from the icon theme, and there is no iconPixmap(), textFormat() (the text is plain), setTextInteractionFlags(), aboutQt() or open() with a receiver. The texts of the buttons are not translated. exec() returns the index of a custom button among buttons() rather than an opaque value.

Member Type Documentation

enum QMessageBox::ButtonRole

The role of a button; the same as QDialogButtonBox::ButtonRole.

ConstantDescription
QMessageBox::InvalidRoleNot a valid role.
QMessageBox::AcceptRoleAccepts, such as OK.
QMessageBox::RejectRoleRejects, such as Cancel.
QMessageBox::DestructiveRoleThrows changes away, such as Discard.
QMessageBox::ActionRoleDoes something else.
QMessageBox::HelpRoleAsks for help.
QMessageBox::YesRoleA Yes button.
QMessageBox::NoRoleA No button.
QMessageBox::ResetRoleResets to defaults.
QMessageBox::ApplyRoleApplies changes.

enum QMessageBox::Icon

The icon shown beside the text.

ConstantDescription
QMessageBox::NoIconNo icon.
QMessageBox::InformationA message that needs no answer: a blue round sign with "i".
QMessageBox::WarningA warning: a yellow triangle with "!".
QMessageBox::CriticalA serious problem: a red round sign with a cross.
QMessageBox::QuestionA question: a blue round sign with "?".

enum QMessageBox::StandardButton

The standard buttons; the same as QDialogButtonBox::StandardButton. StandardButtons is a typedef for QFlags<StandardButton>.

ConstantDescription
QMessageBox::NoButtonNo button.
QMessageBox::OkOK.
QMessageBox::SaveSave.
QMessageBox::SaveAllSave All.
QMessageBox::OpenOpen.
QMessageBox::YesYes.
QMessageBox::YesToAllYes to All.
QMessageBox::NoNo.
QMessageBox::NoToAllNo to All.
QMessageBox::AbortAbort.
QMessageBox::RetryRetry.
QMessageBox::IgnoreIgnore.
QMessageBox::CloseClose.
QMessageBox::CancelCancel.
QMessageBox::DiscardDiscard.
QMessageBox::HelpHelp.
QMessageBox::ApplyApply.
QMessageBox::ResetReset.
QMessageBox::RestoreDefaultsRestore Defaults.

typedef QMessageBox::StandardButtons

The StandardButtons type is a typedef for QFlags<StandardButton>. It stores an OR combination of StandardButton values.

Member Function Documentation

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

Constructs a message box with no text, no icon and no buttons, with the given parent.

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

Constructs a message box with the given icon, window title, text, standard buttons, parent and window flags f.

[override] QMessageBox::~QMessageBox()

Destroys the message box.

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

Shows a message box with an OK button that tells about the application, with the given parent, window title and text, and waits for the user.

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

Adds button with the given role.

See also removeButton().

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

This is an overloaded function.

Creates a push button with the given text and role, adds it and returns it.

QPushButton * QMessageBox::addButton(StandardButton button)

This is an overloaded function.

Adds the standard button and returns it.

QAbstractButton * QMessageBox::button(StandardButton which) const

Returns the button of the standard button which, or nullptr.

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

This signal is emitted when button of the message box is clicked, before the box closes.

ButtonRole QMessageBox::buttonRole(QAbstractButton *button) const

Returns the role of button, or InvalidRole if it is not in the message box.

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

Returns the buttons of the message box.

QCheckBox * QMessageBox::checkBox() const

Returns the check box of the message box, or nullptr.

See also setCheckBox().

QAbstractButton * QMessageBox::clickedButton() const

Returns the button that the user clicked, or nullptr if none was clicked yet.

[override, protected] void QMessageBox::closeEvent(QCloseEvent *event)

Reimplements QDialog::closeEvent().

Closing the window clicks the escape button; without one the window stays open.

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

Shows a message box with the Critical icon and waits for the user. Returns the standard button that was clicked, or NoButton. parent, title, text, buttons and defaultButton set up the box.

QPushButton * QMessageBox::defaultButton() const

Returns the button that was made the default with setDefaultButton(), or nullptr.

QString QMessageBox::detailedText() const

Returns the detailed text, or an empty string if there is none.

See also setDetailedText().

QAbstractButton * QMessageBox::escapeButton() const

Returns the button that was set with setEscapeButton(), or nullptr.

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

Reimplements QDialog::exec().

Returns the standard button that was clicked, or the index of a custom button among buttons(), or -1 if no button was clicked.

Icon QMessageBox::icon() const

Returns the icon of the message box, NoIcon by default.

See also setIcon().

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

Shows a message box with the Information icon and waits for the user. Returns the standard button that was clicked, or NoButton. parent, title, text, buttons and defaultButton set up the box.

QString QMessageBox::informativeText() const

Returns the text shown under the message.

See also setInformativeText().

[override, protected] void QMessageBox::keyPressEvent(QKeyEvent *event)

Reimplements QDialog::keyPressEvent().

Escape clicks the escape button; without one it does nothing.

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

Shows a message box with the Question icon and waits for the user. Returns the standard button that was clicked, or NoButton. parent, title, text, buttons and defaultButton set up the box.

void QMessageBox::removeButton(QAbstractButton *button)

Removes button from the message box without deleting it.

See also addButton().

void QMessageBox::setCheckBox(QCheckBox *cb)

Puts the check box cb under the text, such as "Do not ask again", or removes the check box with nullptr. The message box takes the check box as a child.

See also checkBox().

void QMessageBox::setDefaultButton(QPushButton *button)

Makes button the default button: the one that Return clicks.

See also defaultButton().

void QMessageBox::setDefaultButton(StandardButton button)

This is an overloaded function.

Makes the standard button the default button.

void QMessageBox::setDetailedText(const QString &text)

Sets the detailed text to text. A "Show Details..." button then shows it in a text area under the buttons. An empty text removes the button.

See also detailedText().

void QMessageBox::setEscapeButton(QAbstractButton *button)

Makes button the button that Escape and closing the window click.

See also escapeButton().

void QMessageBox::setEscapeButton(StandardButton button)

This is an overloaded function.

Makes the standard button the escape button.

void QMessageBox::setIcon(Icon icon)

Sets the icon beside the text to icon.

See also icon().

void QMessageBox::setInformativeText(const QString &text)

Sets the text shown under the message, such as what the user can do about it, to text.

See also informativeText().

void QMessageBox::setStandardButtons(StandardButtons buttons)

Makes buttons the standard buttons of the message box.

See also standardButtons().

void QMessageBox::setText(const QString &text)

Sets the text of the message to text. Lines are wrapped to the width of the box.

See also text().

[override, protected] void QMessageBox::showEvent(QShowEvent *event)

Reimplements QDialog::showEvent().

StandardButton QMessageBox::standardButton(QAbstractButton *button) const

Returns which standard button button is, or NoButton.

StandardButtons QMessageBox::standardButtons() const

Returns the standard buttons of the message box.

QString QMessageBox::text() const

Returns the text of the message.

See also setText().

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

Shows a message box with the Warning icon and waits for the user. Returns the standard button that was clicked, or NoButton. parent, title, text, buttons and defaultButton set up the box.