📄 qmessagebox.cpp
字号:
Qt::TextInteractionFlags flags(style()->styleHint(QStyle::SH_MessageBox_TextInteractionFlags, 0, this)); d->label->setTextInteractionFlags(flags); d->buttonBox->setCenterButtons(style()->styleHint(QStyle::SH_MessageBox_CenterButtons, 0, this)); if (d->informativeLabel) d->informativeLabel->setTextInteractionFlags(flags); // intentional fall through } case QEvent::FontChange: case QEvent::ApplicationFontChange:#ifdef Q_WS_MAC { QFont f = font(); f.setBold(true); d->label->setFont(f); }#endif default: break; } QDialog::changeEvent(ev);}/*! \reimp*/void QMessageBox::keyPressEvent(QKeyEvent *e){ Q_D(QMessageBox); if (e->key() == Qt::Key_Escape#ifdef Q_WS_MAC || (e->modifiers() == Qt::ControlModifier && e->key() == Qt::Key_Period)#endif ) { if (d->detectedEscapeButton) {#ifdef Q_WS_MAC d->detectedEscapeButton->animateClick();#else d->detectedEscapeButton->click();#endif } return; }#ifdef Q_OS_WIN if (e == QKeySequence::Copy) { QString separator = QString::fromLatin1("---------------------------\n"); QString textToCopy = separator; separator.prepend(QLatin1String("\n")); textToCopy += windowTitle() + separator; // title textToCopy += d->label->text() + separator; // text if (d->informativeLabel) textToCopy += d->informativeLabel->text() + separator; QString buttonTexts; QList<QAbstractButton *> buttons = d->buttonBox->buttons(); for (int i = 0; i < buttons.count(); i++) { buttonTexts += buttons[i]->text() + QLatin1String(" "); } textToCopy += buttonTexts + separator; qApp->clipboard()->setText(textToCopy); return; }#endif#ifndef QT_NO_SHORTCUT if (!(e->modifiers() & Qt::AltModifier)) { int key = e->key() & ~((int)Qt::MODIFIER_MASK|(int)Qt::UNICODE_ACCEL); if (key) { const QList<QAbstractButton *> buttons = d->buttonBox->buttons(); for (int i = 0; i < buttons.count(); ++i) { QAbstractButton *pb = buttons.at(i); int acc = pb->shortcut() & ~((int)Qt::MODIFIER_MASK|(int)Qt::UNICODE_ACCEL); if (acc == key) { pb->animateClick(); return; } } } }#endif QDialog::keyPressEvent(e);}/*! \reimp*/void QMessageBox::showEvent(QShowEvent *e){ Q_D(QMessageBox); if (d->autoAddOkButton) addButton(Ok); if (d->detailsButton) addButton(d->detailsButton, QMessageBox::ActionRole); d->detectEscapeButton(); d->updateSize();#ifndef QT_NO_ACCESSIBILITY QAccessible::updateAccessibility(this, 0, QAccessible::Alert);#endif#ifdef Q_WS_WIN HMENU systemMenu = GetSystemMenu((HWND)winId(), FALSE); if (!d->detectedEscapeButton) { EnableMenuItem(systemMenu, SC_CLOSE, MF_BYCOMMAND|MF_GRAYED); } else { EnableMenuItem(systemMenu, SC_CLOSE, MF_BYCOMMAND|MF_ENABLED); }#endif QDialog::showEvent(e);}static QMessageBox::StandardButton showNewMessageBox(QWidget *parent, QMessageBox::Icon icon, const QString& title, const QString& text, QMessageBox::StandardButtons buttons, QMessageBox::StandardButton defaultButton){ // necessary for source compatibility with Qt 4.0 and 4.1 // handles (Yes, No) and (Yes|Default, No) if (defaultButton && !(buttons & defaultButton)) return (QMessageBox::StandardButton) QMessageBoxPrivate::showOldMessageBox(parent, icon, title, text, int(buttons), int(defaultButton), 0); QMessageBox msgBox(icon, title, text, QMessageBox::NoButton, parent); QDialogButtonBox *buttonBox = qFindChild<QDialogButtonBox*>(&msgBox); Q_ASSERT(buttonBox != 0); uint mask = QMessageBox::FirstButton; while (mask <= QMessageBox::LastButton) { uint sb = buttons & mask; mask <<= 1; if (!sb) continue; QPushButton *button = msgBox.addButton((QMessageBox::StandardButton)sb); // Choose the first accept role as the default if (msgBox.defaultButton()) continue; if ((defaultButton == QMessageBox::NoButton && buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole) || (defaultButton != QMessageBox::NoButton && sb == uint(defaultButton))) msgBox.setDefaultButton(button); } if (msgBox.exec() == -1) return QMessageBox::Cancel; return msgBox.standardButton(msgBox.clickedButton());}/*! \since 4.2 Opens an information message box with the title \a title and the text \a text. The standard buttons \a buttons is added to the message box. \a defaultButton specifies the button be used as the defaultButton. If the \a defaultButton is set to QMessageBox::NoButton, QMessageBox picks a suitable default automatically. Returns the identity of the standard button that was activated. If \key Esc was pressed, returns the \l{Default and Escape Keys}{escape button} (if any). 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()*/QMessageBox::StandardButton QMessageBox::information(QWidget *parent, const QString &title, const QString& text, StandardButtons buttons, StandardButton defaultButton){ return showNewMessageBox(parent, Information, title, text, buttons, defaultButton);}/*! \since 4.2 Opens a question message box with the title \a title and the text \a text. The standard buttons \a buttons is added to the message box. \a defaultButton specifies the button be used as the defaultButton. If the \a defaultButton is set to QMessageBox::NoButton, QMessageBox picks a suitable default automatically. Returns the identity of the standard button that was activated. If \key Esc was pressed, returns the \l{Default and Escape Keys}{escape button} (if any). 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()*/QMessageBox::StandardButton QMessageBox::question(QWidget *parent, const QString &title, const QString& text, StandardButtons buttons, StandardButton defaultButton){ return showNewMessageBox(parent, Question, title, text, buttons, defaultButton);}/*! \since 4.2 Opens a warning message box with the title \a title and the text \a text. The standard buttons \a buttons is added to the message box. \a defaultButton specifies the button be used as the defaultButton. If the \a defaultButton is set to QMessageBox::NoButton, QMessageBox picks a suitable default automatically. Returns the identity of the standard button that was activated. If \key Esc was pressed, returns the \l{Default and Escape Keys}{escape button} (if any). 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(), information(), critical()*/QMessageBox::StandardButton QMessageBox::warning(QWidget *parent, const QString &title, const QString& text, StandardButtons buttons, StandardButton defaultButton){ return showNewMessageBox(parent, Warning, title, text, buttons, defaultButton);}/*! \since 4.2 Opens a critical message box with the title \a title and the text \a text. The standard buttons \a buttons is added to the message box. \a defaultButton specifies the button be used as the defaultButton. If the \a defaultButton is set to QMessageBox::NoButton, QMessageBox picks a suitable default automatically. Returns the identity of the standard button that was activated. If \key Esc was pressed, returns the \l{Default and Escape Keys}{escape button} (if any). 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(), information()*/QMessageBox::StandardButton QMessageBox::critical(QWidget *parent, const QString &title, const QString& text, StandardButtons buttons, StandardButton defaultButton){ return showNewMessageBox(parent, Critical, title, text, buttons, defaultButton);}/*! Displays a simple about box with title \a title and text \a text. The about box's parent is \a parent. about() looks for a suitable icon in four locations: \list 1 \o It prefers \link QWidget::windowIcon() parent->icon() \endlink if that exists. \o If not, it tries the top-level widget containing \a parent. \o If that fails, it tries the \link QApplication::activeWindow() active window. \endlink \o 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 &title, const QString &text){ QMessageBox mb(title, 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();}/*! Displays a simple message box about Qt, with the given \a title 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 \gui Help menu of an application, as shown in the \l{mainwindows/menus}{Menus} example. QApplication provides this functionality as a slot. \sa QApplication::aboutQt()*/void QMessageBox::aboutQt(QWidget *parent, const QString &title){ QMessageBox mb(parent); QString c = title; 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.addButton(QMessageBox::Ok); mb.exec();}/*! \internal*/QSize QMessageBox::sizeHint() const{ // ### Qt 5: remove return QDialog::sizeHint();}/////////////////////////////////////////////////////////////////////////////////////////// Source and binary compatibility routines for 4.0 and 4.1static QMessageBox::StandardButton newButton(int button){ // this is needed for source compatibility with Qt 4.0 and 4.1 if (button == QMessageBox::NoButton || (button & NewButtonFlag)) return QMessageBox::StandardButton(button & QMessageBox::ButtonMask);#if QT_VERSION < 0x050000 // this is needed for binary compatibility with Qt 4.0 and 4.1 switch (button & Old_ButtonMask) { case Old_Ok: return QMessageBox::Ok; case Old_Cancel: return QMessageBox::Cancel; case Old_Yes: return QMessageBox::Yes; case Old_No: return QMessageBox::No; case Old_Abort: return QMessageBox::Abort; case Old_Retry: return QMessageBox::Retry; case Old_Ignore: return QMessageBox::Ignore; case Old_YesAll: return QMessageBox::YesToAll; case Old_NoAll: return QMessageBox::NoToAll; default: return QMessageBox::NoButton; }#endif}static bool detectedCompat(int button0, int button1, int button2){ if (button0 != 0 && !(button0 & NewButtonFlag)) return true; if (button1 != 0 && !(button1 & NewButtonFlag)) return true; if (button2 != 0 && !(button2 & NewButtonFlag)) return true; return false;}QAbstractButton *QMessageBoxPrivate::findButton(int button0, int button1, int button2, int flags){ Q_Q(QMessageBox); int button = 0; if (button0 & flags) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -