📄 qmessagebox.cpp
字号:
/*! Returns the text of the message box button \a button, or an empty string if the message box does not contain the button. \sa setButtonText()*/QString QMessageBox::buttonText(int button) const{ Q_D(const QMessageBox); int index = d->indexOf(button); return index >= 0 && d->pb[index] ? d->pb[index]->text() : QString();}/*! Sets the text of the message box button \a button to \a text. Setting the text of a button that is not in the message box is silently ignored. \sa buttonText()*/void QMessageBox::setButtonText(int button, const QString &text){ Q_D(QMessageBox); int index = d->indexOf(button); if (index >= 0 && d->pb[index]) { d->pb[index]->setText(text); QResizeEvent e(size(), size()); event(&e); }}/*! \internal Internal slot to handle button clicks.*/void QMessageBoxPrivate::_q_buttonClicked(){ Q_Q(QMessageBox); int reply = 0; const QObject *s = q->sender(); for (int i = 0; i < numButtons; i++) { if (pb[i] == s) reply = button[i]; } q->done(reply);}/*!\reimp*/QSize QMessageBox::sizeHint() const{ Q_D(const QMessageBox); ensurePolished(); d->label->adjustSize(); QSize labelSize(d->label->size()); QSize maxButtonSizeHint; int n = d->numButtons; for (int i = 0; i < n; i++) maxButtonSizeHint = maxButtonSizeHint.expandedTo(d->pb[i]->sizeHint()); int bw = maxButtonSizeHint.width(); int bh = maxButtonSizeHint.height(); int border = bh / 2 - style()->pixelMetric(QStyle::PM_ButtonDefaultIndicator); if (border <= 0) border = 10; int btn_spacing = style()->styleHint(QStyle::SH_MessageBox_UseBorderForButtonSpacing) ? border : 7;#ifndef Q_OS_TEMP int buttons = d->numButtons * bw + (n-1) * btn_spacing; int h = bh;#else int visibleButtons = 0; for (int i = 0; i < d->numButtons; ++i) visibleButtons += d->pb[i]->isVisible() ? 1 : 0; int buttons = visibleButtons == 0 ? 0 : visibleButtons * bw + (visibleButtons-1) * btn_spacing; int h = visibleButtons == 0 ? 0 : bh; n = visibleButtons;#endif if (labelSize.height()) h += labelSize.height() + 3*border; else h += 2*border; int lmargin = 0; if (d->iconLabel->pixmap() && d->iconLabel->pixmap()->width()) { d->iconLabel->adjustSize(); lmargin += d->iconLabel->width() + border; if (h < d->iconLabel->height() + 3*border + bh && n) h = d->iconLabel->height() + 3*border + bh; } int w = qMax(buttons, labelSize.width() + lmargin) + 2*border; QRect screen = QApplication::desktop()->screenGeometry(pos()); if (w > screen.width()) w = screen.width(); return QSize(w,h);}/*!\reimp*/void QMessageBox::resizeEvent(QResizeEvent *){ Q_D(QMessageBox); int i; QSize maxButtonSizeHint; int n = d->numButtons; for (i = 0; i < n; i++) maxButtonSizeHint = maxButtonSizeHint.expandedTo(d->pb[i]->sizeHint()); int bw = maxButtonSizeHint.width(); int bh = maxButtonSizeHint.height();#ifdef Q_OS_TEMP int visibleButtons = 0; for (i = 0; i < n; ++i) visibleButtons += d->pb[i]->isVisible() ? 1 : 0; n = visibleButtons; bw = visibleButtons == 0 ? 0 : bw; bh = visibleButtons == 0 ? 0 : bh;#endif int border = bh / 2 - style()->pixelMetric(QStyle::PM_ButtonDefaultIndicator); if (border <= 0) border = 10; bool useBorder = style()->styleHint(QStyle::SH_MessageBox_UseBorderForButtonSpacing); int btn_spacing = useBorder ? border : 7; int lmargin = 0; d->iconLabel->adjustSize(); bool rtl = layoutDirection() == Qt::RightToLeft; if (rtl) d->iconLabel->move(width() - border - d->iconLabel->width(), border); else d->iconLabel->move(border, border); if (d->iconLabel->pixmap() && d->iconLabel->pixmap()->width()) lmargin += d->iconLabel->width() + border; d->label->setGeometry((rtl ? 0 : lmargin) + border, border, width() - lmargin -2*border, height() - 3*border - bh); int extra_space = (width() - bw*n - 2*border - (n-1)*btn_spacing); if (n) bw = qMin(bw, (width() - 2 *border) / n); if (useBorder) { for (i=0; i<n; i++) d->pb[rtl ? n - i - 1 : i]->setGeometry(border + i*bw + qMax(0,i*btn_spacing + extra_space*(i+1)/(n+1)), height() - border - bh, bw, bh); } else { for (i=0; i<n; i++) d->pb[rtl ? n - i - 1 : i]->setGeometry(border + i*bw + qMax(0,extra_space/2 + i*btn_spacing), height() - border - bh, bw, bh); }}/*!\reimp*/void QMessageBox::keyPressEvent(QKeyEvent *e){ Q_D(QMessageBox); if (e->key() == Qt::Key_Escape) { if (d->escButton >= 0) { QPushButton *pb = d->pb[d->escButton]; pb->animateClick(); e->accept(); return; } }#ifndef QT_NO_SHORTCUT if (!(e->modifiers() & Qt::AltModifier)) { int key = e->key() & ~((int)Qt::MODIFIER_MASK|(int)Qt::UNICODE_ACCEL); if (key) { QList<QPushButton *> list = qFindChildren<QPushButton *>(this); for (int i = 0; i < list.size(); ++i) { QPushButton *pb = list.at(i); int acc = pb->shortcut() & ~((int)Qt::MODIFIER_MASK|(int)Qt::UNICODE_ACCEL); if (acc == key) { emit pb->animateClick(); return; } } } }#endif QDialog::keyPressEvent(e);}/*!\reimp*/void QMessageBox::showEvent(QShowEvent *e){#ifndef QT_NO_ACCESSIBILITY QAccessible::updateAccessibility(this, 0, QAccessible::Alert);#endif QDialog::showEvent(e);}/*!\reimp*/void QMessageBox::closeEvent(QCloseEvent *e){ Q_D(QMessageBox); QDialog::closeEvent(e); if (d->escButton != -1) setResult(d->button[d->escButton]);}/***************************************************************************** Static QMessageBox functions *****************************************************************************//*! \fn int QMessageBox::message(const QString &caption, const QString& text, const QString& buttonText, QWidget *parent, const char *name) \obsolete Opens a modal message box with the given \a caption and showing the given \a text. The message box has a single button which has the given \a buttonText (or tr("OK")). The message box is centred over its \a parent and is called \a name. Use information(), warning(), question(), or critical() instead.*//*! \fn bool QMessageBox::query(const QString &caption, const QString& text, const QString& yesButtonText, const QString& noButtonText, QWidget *parent, const char *name) \obsolete Queries the user using a modal message box with up to two buttons. The message box has the given \a caption (although some window managers don't show it), and shows the given \a text. The left button has the \a yesButtonText (or tr("OK")), and the right button has the \a noButtonText (or isn't shown). The message box is centred over its \a parent and is called \a name. Use information(), question(), warning(), or critical() instead.*//*! Opens an information message box with the caption \a caption and the text \a text. The dialog may have up to three buttons. Each of the buttons, \a button0, \a button1 and \a button2 may be set to 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 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 question(), warning(), critical()*/int QMessageBox::information(QWidget *parent, const QString& caption, const QString& text, int button0, int button1, int button2){ QMessageBox mb(caption, text, Information, button0, button1, button2, parent);#ifdef Q_WS_MAC mb.setFixedSize(mb.sizeHint());#endif return mb.exec();}/*! Opens a question message box with the caption \a caption and the text \a text. The dialog may have up to three buttons. Each of the buttons, \a button0, \a button1 and \a button2 may be set to 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 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::Yes, 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(), warning(), critical()*/int QMessageBox::question(QWidget *parent, const QString& caption, const QString& text, int button0, int button1, int button2){ QMessageBox mb(caption, text, Question, button0, button1, button2, parent);#ifdef Q_WS_MAC mb.setFixedSize(mb.sizeHint());#endif return mb.exec();}/*! Opens a warning message box with the caption \a caption and the text \a text. The dialog may have up to three buttons. Each of the button parameters, \a button0, \a button1 and \a button2 may be set to 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 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(), critical()*/int QMessageBox::warning(QWidget *parent, const QString& caption, const QString& text, int button0, int button1, int button2){ QMessageBox mb(caption, text, Warning, button0, button1, button2, parent);#ifdef Q_WS_MAC mb.setFixedSize(mb.sizeHint());#endif return mb.exec();}/*! Opens a critical message box with the caption \a caption and the text \a text. The dialog may have up to three buttons. Each of the button parameters, \a button0, \a button1 and \a button2 may be set to 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -