📄 qmessagebox.cpp
字号:
: q->layout()->totalMinimumSize().height(); q->setFixedSize(width, height); QCoreApplication::removePostedEvents(q, QEvent::LayoutRequest);}static int oldButton(int button){ switch (button & QMessageBox::ButtonMask) { case QMessageBox::Ok: return Old_Ok; case QMessageBox::Cancel: return Old_Cancel; case QMessageBox::Yes: return Old_Yes; case QMessageBox::No: return Old_No; case QMessageBox::Abort: return Old_Abort; case QMessageBox::Retry: return Old_Retry; case QMessageBox::Ignore: return Old_Ignore; case QMessageBox::YesToAll: return Old_YesAll; case QMessageBox::NoToAll: return Old_NoAll; default: return 0; }}int QMessageBoxPrivate::execReturnCode(QAbstractButton *button){ int ret = buttonBox->standardButton(button); if (ret == QMessageBox::NoButton) { ret = customButtonList.indexOf(button); // if button == 0, correctly sets ret = -1 } else if (compatMode) { ret = oldButton(ret); } return ret;}void QMessageBoxPrivate::_q_buttonClicked(QAbstractButton *button){ Q_Q(QMessageBox);#ifndef QT_NO_TEXTEDIT if (detailsButton && detailsText && button == detailsButton) { detailsButton->setText(detailsText->isHidden() ? detailsText->label(HideLabel) : detailsText->label(ShowLabel)); detailsText->setHidden(!detailsText->isHidden()); updateSize(); } else#endif { clickedButton = button; q->done(execReturnCode(button)); // does not trigger closeEvent }}/*! \class QMessageBox \brief The QMessageBox class provides a modal dialog with a short message, an icon, and buttons laid out depending on the current style. \ingroup dialogs \mainclass Message boxes are used to provide informative messages and to ask simple questions. \section1 Basic Usage The easiest way to pop up a message box in Qt is to call one of the static functions QMessageBox::information(), QMessageBox::question(), QMessageBox::critical(), and QMessageBox::warning(). For example: \code 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); \endcode Buttons are specified by combining StandardButtons using the bitwise OR operator. The order of the buttons on screen is platform-dependent. For example, on Windows, \gui{Save} is displayed to the left of \gui{Cancel}, whereas on Mac OS, the order is reversed. The text part of all message box messages can be either rich text or plain text. With certain strings that contain XML meta characters, the auto-rich text detection may fail, interpreting plain text 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 text format explicitly with setTextFormat(). Note that the Microsoft Windows User Interface Guidelines recommend using the application name as the window's title. The \l{dialogs/standarddialogs}{Standard Dialogs} example shows how to use QMessageBox as well as other built-in Qt dialogs. \section1 Severity Levels QMessageBox supports four severity levels, indicated by an icon: \table \row \o \img qmessagebox-quest.png \o \l Question \o For message boxes that ask a question as part of normal operation. Some style guides recommend using Information for this purpose. \row \o \img qmessagebox-info.png \o \l Information \o For message boxes that are part of normal operation. \row \o \img qmessagebox-warn.png \o \l Warning \o For message boxes that tell the user about unusual errors. \row \o \img qmessagebox-crit.png \o \l Critical \o For message boxes that tell the user about critical errors. \endtable \section1 Advanced Usage If the convenience static functions, such as QMessageBox::information() and QMessageBox::warning(), are not flexible enough for your needs, you can instantiate a QMessageBox on the stack. You can then use addButton() to add buttons with standard or arbitrary text. When using an instance of QMessageBox with standard buttons, you can test the return value of exec() to determine which button was clicked. For example, \code QMessageBox msgBox; msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No); switch (msgBox.exec()) { case QMessageBox::Yes: // yes was clicked break; case QMessageBox::No: // no was clicked break; default: // should never be reached break; } \endcode When using an instance of QMessageBox with custom buttons, you can test the value of clickedButton() after calling exec(). For example, \code 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 } \endcode In the example above, the \gui Connect button is created using the addButton() overload that takes a text and a ButtonRole. The ButtonRole is used by QMessageBox to determine the ordering of the buttons on screen (which varies according to the platform). The text(), icon() and iconPixmap() functions provide access to the current text and pixmap of the message box. The setText(), setIcon() and setIconPixmap() let you change it. The difference between setIcon() and setIconPixmap() is that the former accepts a QMessageBox::Icon and can be used to set standard icons, whereas the latter accepts a QPixmap and can be used to set custom icons. setButtonText() and buttonText() provide access to the buttons. \section1 Default and Escape Keys The default button (i.e., the button that is activated when the user presses \key Enter) can be specified using setDefaultButton(). If none is specified, QMessageBox will try to find one automatically based on the \l{ButtonRole}s of the buttons in the dialog. Similarly, the escape button (the button that is activated when the user presses \key Esc) is specified using setEscapeButton(). If no escape button is specified, QMessageBox attempts to automatically detect an escape button as follows: \list 1 \o If there is only one button, it is made the escape button. \o If there is a \l Cancel button, it is made the escape button. \o On Mac OS X only, if there is exactly one button with the role QMessageBox::RejectRole, it is made the escape button. \endlist When an escape button could not be automatically detected, pressing \key Esc has no effect. \sa QDialogButtonBox, {fowler}{GUI Design Handbook: Message Box}, {Standard Dialogs Example}, {Application Example}*//*! \enum QMessageBox::StandardButton \since 4.2 These enums describe flags for standard buttons. Each button has a defined \l ButtonRole. \value Ok An "OK" button defined with the \l AcceptRole. \value Open A "Open" button defined with the \l AcceptRole. \value Save A "Save" button defined with the \l AcceptRole. \value Cancel A "Cancel" button defined with the \l RejectRole. \value Close A "Close" button defined with the \l RejectRole. \value Discard A "Discard" or "Don't Save" button, depending on the platform, defined with the \l DestructiveRole. \value Apply An "Apply" button defined with the \l ApplyRole. \value Reset A "Reset" button defined with the \l ResetRole. \value RestoreDefaults A "Restore Defaults" button defined with the \l ResetRole. \value Help A "Help" button defined with the \l HelpRole. \value SaveAll A "Save All" button defined with the \l AcceptRole. \value Yes A "Yes" button defined with the \l YesRole. \value YesToAll A "Yes to All" button defined with the \l YesRole. \value No A "No" button defined with the \l NoRole. \value NoToAll A "No to All" button defined with the \l NoRole. \value Abort An "Abort" button defined with the \l RejectRole. \value Retry A "Retry" button defined with the \l AcceptRole. \value Ignore An "Ignore" button defined with the \l AcceptRole. \value NoButton An invalid button. \omitvalue FirstButton \omitvalue LastButton The following values are obsolete: \value YesAll Use YesToAll instead. \value NoAll Use NoToAll instead. \value Default Use the \c defaultButton argument of information(), warning(), etc. instead, or call setDefaultButton(). \value Escape Call setEscapeButton() instead. \value FlagMask \value ButtonMask \sa ButtonRole, standardButtons*//*! Constructs a message box with no text and no buttons. If \a parent is 0, the message box becomes an application-global modal dialog box. If \a parent is a widget, the message box becomes modal relative to \a parent. The \a parent argument is passed to the QDialog constructor.*/QMessageBox::QMessageBox(QWidget *parent): QDialog(*new QMessageBoxPrivate, parent, Qt::MSWindowsFixedSizeDialogHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint){ Q_D(QMessageBox); d->init();}/*! Constructs a message box with the given \a icon, \a title, \a text, and standard \a buttons. (Buttons can also be added at any time using addButton().) If \a parent is 0, the message box becomes an application-global modal dialog box. If \a parent is a widget, the message box becomes modal relative to \a parent. The \a parent and \a f arguments are passed to the QDialog constructor. \sa setWindowTitle(), setText(), setIcon(), setStandardButtons()*/QMessageBox::QMessageBox(Icon icon, const QString &title, const QString &text, StandardButtons buttons, QWidget *parent, Qt::WindowFlags f): QDialog(*new QMessageBoxPrivate, parent, f | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint){ Q_D(QMessageBox); d->init(title, text); setIcon(icon); if (buttons != NoButton) setStandardButtons(buttons);}/*! Destroys the message box.*/QMessageBox::~QMessageBox(){}/*! \since 4.2 Adds the given \a button to the message box with the specified \a role. \sa removeButton(), button(), setStandardButtons()*/void QMessageBox::addButton(QAbstractButton *button, ButtonRole role){ Q_D(QMessageBox); if (!button) return; removeButton(button); d->buttonBox->addButton(button, (QDialogButtonBox::ButtonRole)role); d->customButtonList.append(button); d->autoAddOkButton = false;}/*! \since 4.2 \overload Creates a button with the given \a text, adds it to the message box for the specified \a role, and returns it.*/QPushButton *QMessageBox::addButton(const QString& text, ButtonRole role){ Q_D(QMessageBox); QPushButton *pushButton = new QPushButton(text); addButton(pushButton, role); d->updateSize(); return pushButton;}/*! \since 4.2 \overload Adds a standard \a button to the message box if it is valid to do so, and returns the push button. \sa setStandardButtons()*/QPushButton *QMessageBox::addButton(StandardButton button){ Q_D(QMessageBox); QPushButton *pushButton = d->buttonBox->addButton((QDialogButtonBox::StandardButton)button); if (pushButton) d->autoAddOkButton = false; return pushButton;}/*! \since 4.2 Removes \a button from the button box without deleting it. \sa addButton(), setStandardButtons()*/void QMessageBox::removeButton(QAbstractButton *button){ Q_D(QMessageBox); d->customButtonList.removeAll(button); if (d->escapeButton == button) d->escapeButton = 0; if (d->defaultButton == button) d->defaultButton = 0; d->buttonBox->removeButton(button); d->updateSize();}/*! \property QMessageBox::standardButtons \brief collection of standard buttons in the message box \since 4.2 This property controls which standard buttons are used by the message box. \sa addButton()*/void QMessageBox::setStandardButtons(StandardButtons buttons){ Q_D(QMessageBox); d->buttonBox->setStandardButtons(QDialogButtonBox::StandardButtons(int(buttons))); QList<QAbstractButton *> buttonList = d->buttonBox->buttons(); if (!buttonList.contains(d->escapeButton)) d->escapeButton = 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -