📄 qdialog.cpp
字号:
}#endif/*! Returns the modal dialog's result code, \c Accepted or \c Rejected. Do not call this function if the dialog was constructed with the Qt::WA_DeleteOnClose attribute.*/int QDialog::result() const{ Q_D(const QDialog); return d->rescode;}/*! \fn void QDialog::setResult(int i) Sets the modal dialog's result code to \a i.*/void QDialog::setResult(int r){ Q_D(QDialog); d->rescode = r;}/*! Shows the dialog as a \l{QDialog#Modal Dialogs}{modal dialog}, blocking until the user closes it. The function returns a \l DialogCode result. If the dialog is \l{Qt::ApplicationModal}{application modal}, users cannot interact with any other window in the same application until they close the dialog. If the dialog is \l{Qt::ApplicationModal}{window modal}, only interaction with the parent window is blocked while the dialog is open. By default, the dialog is application modal. \sa show(), result(), setWindowModality()*/int QDialog::exec(){ Q_D(QDialog); if (d->eventLoop) { qWarning("QDialog::exec: Recursive call detected"); return -1; } bool deleteOnClose = testAttribute(Qt::WA_DeleteOnClose); setAttribute(Qt::WA_DeleteOnClose, false); bool wasShowModal = testAttribute(Qt::WA_ShowModal); setAttribute(Qt::WA_ShowModal, true); setResult(0); show(); QEventLoop eventLoop; d->eventLoop = &eventLoop; QPointer<QDialog> guard = this; (void) eventLoop.exec(); if (guard.isNull()) return QDialog::Rejected; d->eventLoop = 0; setAttribute(Qt::WA_ShowModal, wasShowModal); int res = result(); if (deleteOnClose) delete this; return res;}/*! Closes the dialog and sets its result code to \a r. If this dialog is shown with exec(), done() causes the local event loop to finish, and exec() to return \a r. As with QWidget::close(), done() deletes the dialog if the Qt::WA_DeleteOnClose flag is set. If the dialog is the application's main widget, the application terminates. If the dialog is the last window closed, the QApplication::lastWindowClosed() signal is emitted. \sa accept(), reject(), QApplication::activeWindow(), QApplication::quit()*/void QDialog::done(int r){ Q_D(QDialog); hide(); setResult(r); d->close_helper(QWidgetPrivate::CloseNoEvent); emit finished(r); if (r == Accepted) emit accepted(); else if (r == Rejected) emit rejected();}/*! Hides the modal dialog and sets the result code to \c Accepted. \sa reject() done()*/void QDialog::accept(){ done(Accepted);}/*! Hides the modal dialog and sets the result code to \c Rejected. \sa accept() done()*/void QDialog::reject(){ done(Rejected);}/*! \reimp */bool QDialog::eventFilter(QObject *o, QEvent *e){ return QWidget::eventFilter(o, e);}/***************************************************************************** Event handlers *****************************************************************************//*! \reimp */void QDialog::contextMenuEvent(QContextMenuEvent *e){#if defined(QT_NO_WHATSTHIS) || defined(QT_NO_MENU) Q_UNUSED(e);#else QWidget *w = childAt(e->pos()); if (!w) { w = rect().contains(e->pos()) ? this : 0; if (!w) return; } while (w && w->whatsThis().size() == 0 && !w->testAttribute(Qt::WA_CustomWhatsThis)) w = w->isWindow() ? 0 : w->parentWidget(); if (w) { QMenu p; QAction *wt = p.addAction(tr("What's This?")); if (p.exec(e->globalPos()) == wt) { QHelpEvent e(QEvent::WhatsThis, w->rect().center(), w->mapToGlobal(w->rect().center())); QApplication::sendEvent(w, &e); } }#endif}/*! \reimp */void QDialog::keyPressEvent(QKeyEvent *e){ // Calls reject() if Escape is pressed. Simulates a button // click for the default button if Enter is pressed. Move focus // for the arrow keys. Ignore the rest.#ifdef Q_WS_MAC if(e->modifiers() == Qt::ControlModifier && e->key() == Qt::Key_Period) { reject(); } else#endif if (!e->modifiers() || (e->modifiers() & Qt::KeypadModifier && e->key() == Qt::Key_Enter)) { switch (e->key()) { case Qt::Key_Enter: case Qt::Key_Return: { QList<QPushButton*> list = qFindChildren<QPushButton*>(this); for (int i=0; i<list.size(); ++i) { QPushButton *pb = list.at(i); if (pb->isDefault() && pb->isVisible()) { if (pb->isEnabled()) pb->click(); return; } } } break; case Qt::Key_Escape: reject(); break; case Qt::Key_Up: case Qt::Key_Left: if (focusWidget() && (focusWidget()->focusPolicy() == Qt::StrongFocus || focusWidget()->focusPolicy() == Qt::WheelFocus)) { e->ignore(); break; } // call ours, since c++ blocks us from calling the one // belonging to focusWidget(). focusNextPrevChild(false); break; case Qt::Key_Down: case Qt::Key_Right: if (focusWidget() && (focusWidget()->focusPolicy() == Qt::StrongFocus || focusWidget()->focusPolicy() == Qt::WheelFocus)) { e->ignore(); break; } focusNextPrevChild(true); break; default: e->ignore(); return; } } else { e->ignore(); }}/*! \reimp */void QDialog::closeEvent(QCloseEvent *e){#ifndef QT_NO_WHATSTHIS if (isModal() && QWhatsThis::inWhatsThisMode()) QWhatsThis::leaveWhatsThisMode();#endif if (isVisible()) reject(); else e->accept();}#ifdef Q_OS_TEMP/*! \internal \reimp*/bool QDialog::event(QEvent *e){ switch (e->type()) { case QEvent::OkRequest: case QEvent::HelpRequest: { QString bName = (e->type() == QEvent::OkRequest) ? qApp->translate("QMessageBox", mb_texts[QMessageBox::Ok]) : qApp->translate("QMessageBox", "Help"); QList<QPushButton*> list = qFindChildren<QPushButton*>(this); for (int i=0; i<list.size(); ++i) { QPushButton *pb = list.at(i); if (pb->text() == bName) { if (pb->isEnabled()) pb->click(); return pb->isEnabled(); } } } default: break; } return QWidget::event(e);}#endif/***************************************************************************** Geometry management. *****************************************************************************//*! \reimp*/void QDialog::setVisible(bool visible){ Q_D(QDialog); if (visible) { if (testAttribute(Qt::WA_WState_ExplicitShowHide) && !testAttribute(Qt::WA_WState_Hidden)) return;#ifdef Q_OS_TEMP hideSpecial();#endif if (!testAttribute(Qt::WA_Moved)) { Qt::WindowStates state = windowState(); adjustPosition(parentWidget()); setAttribute(Qt::WA_Moved, false); // not really an explicit position if (state != windowState()) setWindowState(state); } QWidget::setVisible(visible); showExtension(d->doShowExtension); QWidget *fw = window()->focusWidget(); if (!fw) fw = this; /* The following block is to handle a special case, and does not really follow propper logic in concern of autoDefault and TAB order. However, it's here to ease usage for the users. If a dialog has a default QPushButton, and first widget in the TAB order also is a QPushButton, then we give focus to the main default QPushButton. This simplifies code for the developers, and actually catches most cases... If not, then they simply have to use [widget*]->setFocus() themselves... */ if (d->mainDef && fw->focusPolicy() == Qt::NoFocus) { QWidget *first = fw; while ((first = first->nextInFocusChain()) != fw && first->focusPolicy() == Qt::NoFocus) ; if (first != d->mainDef && qobject_cast<QPushButton*>(first)) d->mainDef->setFocus(); } if (!d->mainDef && isWindow()) { QWidget *w = fw; while ((w = w->nextInFocusChain()) != fw) { QPushButton *pb = qobject_cast<QPushButton *>(w); if (pb && pb->autoDefault() && pb->focusPolicy() != Qt::NoFocus) { pb->setDefault(true); break; } } } if (fw && !fw->hasFocus()) { QFocusEvent e(QEvent::FocusIn, Qt::TabFocusReason); QApplication::sendEvent(fw, &e); }#ifndef QT_NO_ACCESSIBILITY QAccessible::updateAccessibility(this, 0, QAccessible::DialogStart);#endif } else { if (testAttribute(Qt::WA_WState_ExplicitShowHide) && testAttribute(Qt::WA_WState_Hidden)) return;#ifndef QT_NO_ACCESSIBILITY if (isVisible()) QAccessible::updateAccessibility(this, 0, QAccessible::DialogEnd);#endif // Reimplemented to exit a modal event loop when the dialog is hidden. QWidget::setVisible(visible); if (d->eventLoop) d->eventLoop->exit(); }#ifdef Q_WS_WIN if (d->mainDef && isActiveWindow()) { BOOL snapToDefault = false; if ( QT_WA_INLINE( SystemParametersInfo(SPI_GETSNAPTODEFBUTTON, 0, &snapToDefault, 0) , SystemParametersInfoA(SPI_GETSNAPTODEFBUTTON, 0, &snapToDefault, 0) )) { if (snapToDefault) QCursor::setPos(d->mainDef->mapToGlobal(d->mainDef->rect().center())); } }#endif}/*!\reimp */void QDialog::showEvent(QShowEvent *event){ if (!event->spontaneous() && !testAttribute(Qt::WA_Moved)) { Qt::WindowStates state = windowState(); adjustPosition(parentWidget()); setAttribute(Qt::WA_Moved, false); // not really an explicit position if (state != windowState()) setWindowState(state); }}/*! \internal */void QDialog::adjustPosition(QWidget* w){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -