📄 qmessagebox.cpp
字号:
QMessageBox::QMessageBox(QWidget *parent) : QDialog(*new QMessageBoxPrivate, parent, Qt::MSWindowsFixedSizeDialogHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint){ Q_D(QMessageBox); setModal(true); d->init(Ok, 0, 0);}/*! Constructs a message box with a \a caption, a \a text, an \a icon, and up to three buttons. The \a icon must be one of the following: \list \i QMessageBox::NoIcon \i QMessageBox::Question \i QMessageBox::Information \i QMessageBox::Warning \i QMessageBox::Critical \endlist Each button, \a button0, \a button1 and \a button2, can have one of the following values: \list \i QMessageBox::NoButton \i QMessageBox::Ok \i QMessageBox::Cancel \i QMessageBox::Yes \i QMessageBox::No \i QMessageBox::Abort \i QMessageBox::Retry \i QMessageBox::Ignore \i QMessageBox::YesAll \i QMessageBox::NoAll \endlist Use QMessageBox::NoButton for the later parameters to have fewer than three buttons in your message box. If you don't specify any buttons at all, QMessageBox will provide an Ok button. One of the buttons can be OR-ed with the QMessageBox::Default flag to make it the default button (clicked when Enter is pressed). One of the buttons can be OR-ed with the QMessageBox::Escape flag to make it the cancel or close button (clicked when Escape is pressed). \quotefromfile snippets/dialogs/dialogs.cpp \skipto // hardware failure \skipto QMessageBox mb("Application Name", \printuntil // try again 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()*/QMessageBox::QMessageBox(const QString& caption, const QString &text, Icon icon, int button0, int button1, int button2, QWidget *parent, Qt::WFlags f) : QDialog(*new QMessageBoxPrivate, parent, f | Qt::MSWindowsFixedSizeDialogHint | Qt::WindowTitleHint | Qt::WindowSystemMenuHint){ Q_D(QMessageBox); d->init(button0, button1, button2);#ifdef Q_WS_MAC // Make our message box look a little more mac like. QString finalText = QLatin1String("<p><b>") + caption + QLatin1String("</b></p>"); if (Qt::mightBeRichText(text)) finalText += QLatin1String("<br><br>") + text; else finalText += Qt::convertFromPlainText(text); setText(finalText);#else setWindowTitle(caption); setText(text);#endif setIcon(icon);}/*! Destroys the message box.*/QMessageBox::~QMessageBox(){}static QString * translatedTextAboutQt = 0;void QMessageBoxPrivate::init(int button0, int button1, int button2){ Q_Q(QMessageBox); if (!translatedTextAboutQt) { translatedTextAboutQt = new QString;#if defined(QT_NON_COMMERCIAL) QT_NC_MSGBOX#else *translatedTextAboutQt = QMessageBox::tr( "<h3>About Qt</h3>" "%1<p>Qt is a C++ toolkit for cross-platform " "application development.</p>" "<p>Qt provides single-source " "portability across MS Windows, Mac OS X, " "Linux, and all major commercial Unix variants. Qt is also" " available for embedded devices as Qtopia Core.</p>" "<p>Qt is a Trolltech product. See " "<tt>http://www.trolltech.com/qt/</tt> for more information.</p>" )#if QT_EDITION != QT_EDITION_OPENSOURCE .arg(QMessageBox::tr("<p>This program uses Qt version %1.</p>"))#else .arg(QMessageBox::tr("<p>This program uses Qt Open Source Edition version %1.</p>" "<p>Qt Open Source Edition is intended for the development " "of Open Source applications. You need a commercial Qt " "license for development of proprietary (closed source) " "applications.</p>" "<p>Please see <tt>http://www.trolltech.com/company/model.html</tt> " "for an overview of Qt licensing.</p>"))#endif .arg(QT_VERSION_STR);#endif } label = new QLabel(q); label->setObjectName(QLatin1String("qt_msgbox_label")); label->setAlignment(Qt::AlignTop|Qt::AlignLeft); if ((button2 && !button1) || (button1 && !button0)) { qWarning("QMessageBox: Inconsistent button parameters"); button0 = button1 = button2 = 0; } icon = QMessageBox::NoIcon; iconLabel = new QLabel(q); iconLabel->setObjectName(QLatin1String("qt_msgbox_icon_label")); iconLabel->setPixmap(QPixmap()); numButtons = 0; button[0] = button0; button[1] = button1; button[2] = button2; defButton = -1; escButton = -1; int i; for (i=0; i<3; i++) { int b = button[i]; if ((b & QMessageBox::Default)) { if (defButton >= 0) { qWarning("QMessageBox: There can be at most one default button"); } else { defButton = i; } } if ((b & QMessageBox::Escape)) { if (escButton >= 0) { qWarning("QMessageBox: There can be at most one escape button"); } else { escButton = i; } } b &= QMessageBox::ButtonMask; if (b == 0) { if (i == 0) // no buttons, add an Ok button b = QMessageBox::Ok; } else if (b < 0 || b > LastButton) { qWarning("QMessageBox: Invalid button specifier"); b = QMessageBox::Ok; } else { if (i > 0 && button[i-1] == 0) { qWarning("QMessageBox: Inconsistent button parameters; " "button %d defined but not button %d", i+1, i); b = 0; } } button[i] = b; if (b) numButtons++; } for (i=0; i<3; i++) { if (i >= numButtons) { pb[i] = 0; } else { pb[i] = new QPushButton(QMessageBox::tr(mb_texts[button[i]]), q); pb[i]->setObjectName(mb_texts[button[i]]); if (defButton == i) { pb[i]->setDefault(true); pb[i]->setFocus(); } pb[i]->setAutoDefault(true); pb[i]->setFocusPolicy(Qt::StrongFocus); q->connect(pb[i], SIGNAL(clicked()), SLOT(_q_buttonClicked())); } }}int QMessageBoxPrivate::indexOf(int button) const{ int index = -1; for (int i = 0; i < numButtons; i++) { if (this->button[i] == button) { index = i; break; } } return index;}/*! \property QMessageBox::text \brief the message box text to be displayed. The text will be interpreted either as a plain text or as rich text, depending on the text format setting (\l 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. \sa textFormat*/QString QMessageBox::text() const{ Q_D(const QMessageBox); return d->label->text();}void QMessageBox::setText(const QString &text){ Q_D(QMessageBox); d->label->setText(text); bool wordwrap = d->label->textFormat() == Qt::RichText || (d->label->textFormat() == Qt::AutoText && Qt::mightBeRichText(text)); d->label->setWordWrap(wordwrap);}/*! \property QMessageBox::icon \brief the message box's icon The icon of the message box can be one of the following predefined icons: \list \i QMessageBox::NoIcon \i QMessageBox::Question \i QMessageBox::Information \i QMessageBox::Warning \i QMessageBox::Critical \endlist The actual pixmap used for displaying the icon depends on the current \link QWidget::style() GUI style\endlink. You can also set a custom pixmap icon using the \l QMessageBox::iconPixmap property. The default icon is QMessageBox::NoIcon. \sa iconPixmap*/QMessageBox::Icon QMessageBox::icon() const{ Q_D(const QMessageBox); return d->icon;}void QMessageBox::setIcon(Icon icon){ Q_D(QMessageBox); setIconPixmap(standardIcon(icon)); d->icon = icon;}#ifdef QT3_SUPPORT/*! \compat Constructs a message box with the given \a parent, \a name, and window flags, \a f. The window title is specified by \a caption, and the message box displays message text and an icon specified by \a text and \a icon. The buttons that the user can access to respond to the message are defined by \a button0, \a button1, and \a button2.*/QMessageBox::QMessageBox(const QString& caption, const QString &text, Icon icon, int button0, int button1, int button2, QWidget *parent, const char *name, bool modal, Qt::WFlags f) : QDialog(*new QMessageBoxPrivate, parent, f | Qt::WStyle_Customize | Qt::WStyle_DialogBorder | Qt::WStyle_Title | Qt::WStyle_SysMenu){ Q_D(QMessageBox); setObjectName(QString::fromAscii(name)); setModal(modal); d->init(button0, button1, button2);#ifdef Q_WS_MAC setText("<p><b>" + caption + "</b></p><p>" + text + "</p>");#else setWindowTitle(caption); setText(text);#endif setIcon(icon);}/*! Constructs a message box with the given \a parent and \a name.*/QMessageBox::QMessageBox(QWidget *parent, const char *name) : QDialog(*new QMessageBoxPrivate, parent, Qt::WStyle_Customize | Qt::WStyle_DialogBorder | Qt::WStyle_Title | Qt::WStyle_SysMenu){ Q_D(QMessageBox); setObjectName(QString::fromAscii(name)); setModal(true); d->init(Ok, 0, 0);}/*! \obsolete Returns the pixmap used for a standard icon. This allows the pixmaps to be used in more complex message boxes. \a icon specifies the required icon, e.g. QMessageBox::Information, QMessageBox::Warning or QMessageBox::Critical. \a style is unused.*/QPixmap QMessageBox::standardIcon(Icon icon, Qt::GUIStyle style){ Q_UNUSED(style); return QMessageBox::standardIcon(icon);}#endif/*! Returns the pixmap used for a standard icon. This allows the pixmaps to be used in more complex message boxes. \a icon specifies the required icon, e.g. QMessageBox::Question, QMessageBox::Information, QMessageBox::Warning or QMessageBox::Critical.*/QPixmap QMessageBox::standardIcon(Icon icon){ QPixmap pm; switch (icon) { case Information: pm = QApplication::style()->standardPixmap(QStyle::SP_MessageBoxInformation); break; case Warning: pm = QApplication::style()->standardPixmap(QStyle::SP_MessageBoxWarning); break; case Critical: pm = QApplication::style()->standardPixmap(QStyle::SP_MessageBoxCritical); break; case Question: pm = QApplication::style()->standardPixmap(QStyle::SP_MessageBoxQuestion); default: break; } return pm;}/*! \property QMessageBox::iconPixmap \brief the current icon 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. \sa icon*/QPixmap QMessageBox::iconPixmap() const{ Q_D(const QMessageBox); return *d->iconLabel->pixmap();}void QMessageBox::setIconPixmap(const QPixmap &pixmap){ Q_D(QMessageBox); d->iconLabel->setPixmap(pixmap); d->icon = NoIcon;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -