📄 qmessagebox.cpp
字号:
If you don't want all three buttons, set the last button, or last two buttons to QMessageBox::NoButton. One button can be OR-ed with QMessageBox::Default, and one button can be OR-ed with QMessageBox::Escape. Returns the identity (QMessageBox::Ok, or QMessageBox::No, etc.) of the button that was clicked. 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. \sa information(), question(), warning()*/int QMessageBox::critical(QWidget *parent, const QString& caption, const QString& text, int button0, int button1, int button2){ QMessageBox mb(caption, text, Critical, button0, button1, button2, parent);#ifdef Q_WS_MAC mb.setFixedSize(mb.sizeHint());#endif return mb.exec();}/*! Displays a simple about box with caption \a caption and text \a text. The about box's parent is \a parent. about() looks for a suitable icon in four locations: \list 1 \i It prefers \link QWidget::windowIcon() parent->icon() \endlink if that exists. \i If not, it tries the top-level widget containing \a parent. \i If that fails, it tries the \link QApplication::activeWindow() active window. \endlink \i As a last resort it uses the Information icon. \endlist The about box has a single button labelled "OK". \sa QWidget::windowIcon() QApplication::activeWindow()*/void QMessageBox::about(QWidget *parent, const QString &caption, const QString& text){ QMessageBox mb(caption, text, Information, Ok + Default, 0, 0, parent); QIcon icon = mb.windowIcon(); QSize size = icon.actualSize(QSize(64, 64)); mb.setIconPixmap(icon.pixmap(size)); mb.exec();}/*! \reimp*/void QMessageBox::changeEvent(QEvent *ev){ Q_D(QMessageBox); if(ev->type() == QEvent::StyleChange) { if (d->icon != NoIcon) { // Reload icon for new style setIcon(d->icon); } } QWidget::changeEvent(ev);}static int textBox(QWidget *parent, QMessageBox::Icon severity, const QString& caption, const QString& text, const QString& button0Text, const QString& button1Text, const QString& button2Text, int defaultButtonNumber, int escapeButtonNumber){ int b[3]; b[0] = 1; b[1] = button1Text.isEmpty() ? 0 : 2; b[2] = button2Text.isEmpty() ? 0 : 3; int i; for(i=0; i<3; i++) { if (b[i] && defaultButtonNumber == i) b[i] += QMessageBox::Default; if (b[i] && escapeButtonNumber == i) b[i] += QMessageBox::Escape; } QMessageBox mb(caption, text, severity, b[0], b[1], b[2], parent); if (button0Text.isEmpty()) mb.setButtonText(1, QMessageBox::tr(mb_texts[QMessageBox::Ok])); else mb.setButtonText(1, button0Text); if (b[1]) mb.setButtonText(2, button1Text); if (b[2]) mb.setButtonText(3, button2Text);#ifndef QT_NO_CURSOR mb.setCursor(Qt::ArrowCursor);#endif#ifdef Q_WS_MAC mb.setFixedSize(mb.sizeHint());#endif return mb.exec() - 1;}/*! \overload Displays an information message box with caption \a caption, text \a text and one, two or three buttons. Returns the index of the button that was clicked (0, 1 or 2). \a button0Text is the text of the first button, and is optional. If \a button0Text is not supplied, "OK" (translated) will be used. \a button1Text is the text of the second button, and is optional. \a button2Text is the text of the third button, and is optional. \a defaultButtonNumber (0, 1 or 2) is the index of the default button; pressing Return or Enter is the same as clicking the default button. It defaults to 0 (the first button). \a escapeButtonNumber is the index of the Escape button; pressing Escape is the same as clicking this button. It defaults to -1; supply 0, 1 or 2 to make pressing Escape equivalent to clicking the relevant button. 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. Note: If you do not specify an Escape button then if the Escape button is pressed then -1 will be returned. It is suggested that you specify an Escape button to prevent this from happening. \sa question(), warning(), critical()*/int QMessageBox::information(QWidget *parent, const QString &caption, const QString& text, const QString& button0Text, const QString& button1Text, const QString& button2Text, int defaultButtonNumber, int escapeButtonNumber){ return textBox(parent, Information, caption, text, button0Text, button1Text, button2Text, defaultButtonNumber, escapeButtonNumber);}/*! \overload Displays a question message box with caption \a caption, text \a text and one, two or three buttons. Returns the index of the button that was clicked (0, 1 or 2). \a button0Text is the text of the first button, and is optional. If \a button0Text is not supplied, "OK" (translated) will be used. \a button1Text is the text of the second button, and is optional. \a button2Text is the text of the third button, and is optional. \a defaultButtonNumber (0, 1 or 2) is the index of the default button; pressing Return or Enter is the same as clicking the default button. It defaults to 0 (the first button). \a escapeButtonNumber is the index of the Escape button; pressing Escape is the same as clicking this button. It defaults to -1; supply 0, 1 or 2 to make pressing Escape equivalent to clicking the relevant button. 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. Note: If you do not specify an Escape button then if the Escape button is pressed then -1 will be returned. It is suggested that you specify an Escape button to prevent this from happening. \sa information(), warning(), critical()*/int QMessageBox::question(QWidget *parent, const QString &caption, const QString& text, const QString& button0Text, const QString& button1Text, const QString& button2Text, int defaultButtonNumber, int escapeButtonNumber){ return textBox(parent, Question, caption, text, button0Text, button1Text, button2Text, defaultButtonNumber, escapeButtonNumber);}/*! \overload Displays a warning message box with a caption, a text, and 1, 2 or 3 buttons. Returns the number of the button that was clicked (0, 1, or 2). \a button0Text is the text of the first button, and is optional. If \a button0Text is not supplied, "OK" (translated) will be used. \a button1Text is the text of the second button, and is optional, and \a button2Text is the text of the third button, and is optional. \a defaultButtonNumber (0, 1 or 2) is the index of the default button; pressing Return or Enter is the same as clicking the default button. It defaults to 0 (the first button). \a escapeButtonNumber is the index of the Escape button; pressing Escape is the same as clicking this button. It defaults to -1; supply 0, 1, or 2 to make pressing Escape equivalent to clicking the relevant button. 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. Note: If you do not specify an Escape button then if the Escape button is pressed then -1 will be returned. It is suggested that you specify an Escape button to prevent this from happening. \sa information(), question(), critical()*/int QMessageBox::warning(QWidget *parent, const QString &caption, const QString& text, const QString& button0Text, const QString& button1Text, const QString& button2Text, int defaultButtonNumber, int escapeButtonNumber){ return textBox(parent, Warning, caption, text, button0Text, button1Text, button2Text, defaultButtonNumber, escapeButtonNumber);}/*! \overload Displays a critical error message box with a caption, a text, and 1, 2 or 3 buttons. Returns the number of the button that was clicked (0, 1 or 2). \a button0Text is the text of the first button, and is optional. If \a button0Text is not supplied, "OK" (translated) will be used. \a button1Text is the text of the second button, and is optional, and \a button2Text is the text of the third button, and is optional. \a defaultButtonNumber (0, 1 or 2) is the index of the default button; pressing Return or Enter is the same as clicking the default button. It defaults to 0 (the first button). \a escapeButtonNumber is the index of the Escape button; pressing Escape is the same as clicking this button. It defaults to -1; supply 0, 1, or 2 to make pressing Escape equivalent to clicking the relevant button. 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. \sa information(), question(), warning()*/int QMessageBox::critical(QWidget *parent, const QString &caption, const QString& text, const QString& button0Text, const QString& button1Text, const QString& button2Text, int defaultButtonNumber, int escapeButtonNumber){ return textBox(parent, Critical, caption, text, button0Text, button1Text, button2Text, defaultButtonNumber, escapeButtonNumber);}#ifndef QT_NO_IMAGEFORMAT_XPM// helperextern void qt_read_xpm_image_or_array(QImageReader *, const char * const *, QImage &);#endif /*! Displays a simple message box about Qt, with caption \a caption and centered over \a parent (if \a parent is not 0). The message includes the version number of Qt being used by the application. This is useful for inclusion in the Help menu of an application. See the examples/menu/menu.cpp example. QApplication provides this functionality as a slot. \sa QApplication::aboutQt()*/void QMessageBox::aboutQt(QWidget *parent, const QString &caption){ QMessageBox mb(parent); QString c = caption; if (c.isEmpty()) c = tr("About Qt"); mb.setWindowTitle(c); mb.setText(*translatedTextAboutQt);#ifndef QT_NO_IMAGEFORMAT_XPM QImage logo(qtlogo_xpm);#else QImage logo;#endif if (qGray(mb.palette().color(QPalette::Active, QPalette::Text).rgb()) > qGray(mb.palette().color(QPalette::Active, QPalette::Base).rgb())) { // light on dark, adjust some colors logo.setColor(0, 0xffffffff); logo.setColor(1, 0xff666666); logo.setColor(2, 0xffcccc66); logo.setColor(4, 0xffcccccc); logo.setColor(6, 0xffffff66); logo.setColor(7, 0xff999999); logo.setColor(8, 0xff3333ff); logo.setColor(9, 0xffffff33); logo.setColor(11, 0xffcccc99); } QPixmap pm = QPixmap::fromImage(logo); if (!pm.isNull()) mb.setIconPixmap(pm); mb.setButtonText(0, tr("OK")); if (mb.d_func()->pb[0]) { mb.d_func()->pb[0]->setAutoDefault(true); mb.d_func()->pb[0]->setFocusPolicy(Qt::StrongFocus); mb.d_func()->pb[0]->setDefault(true); mb.d_func()->pb[0]->setFocus(); } mb.exec();}/*! \property QMessageBox::textFormat \brief the format of the text displayed by the message box The current text format used by the message box. See the \l Qt::TextFormat enum for an explanation of the possible options. The default format is Qt::AutoText. \sa setText()*/Qt::TextFormat QMessageBox::textFormat() const{ Q_D(const QMessageBox); return d->label->textFormat();}void QMessageBox::setTextFormat(Qt::TextFormat format){ Q_D(QMessageBox); d->label->setTextFormat(format); bool wordwrap = format == Qt::RichText || (format == Qt::AutoText && Qt::mightBeRichText(d->label->text())); d->label->setWordWrap(wordwrap);}/*! \macro QT_REQUIRE_VERSION(int argc, char **argv, const char *version) \relates QMessageBox This macro can be used to ensure that the application is run against 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 (e.g., 4.0.2). The \a argc and \a argv parameters are the \c main() function's \c argc and \c argv parameters. The \a version parameter is a string literal that specifies which version of Qt the application requires (e.g., "4.0.2"). Example: \code #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(); } \endcode*/#include "moc_qmessagebox.cpp"#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -