📄 qapplication.cpp
字号:
}void QApplicationPrivate::setFocusWidget(QWidget *focus, Qt::FocusReason reason){ if (focus != focus_widget) { if (focus && (reason == Qt::BacktabFocusReason || reason == Qt::TabFocusReason) && qt_in_tab_key_event) focus->window()->setAttribute(Qt::WA_KeyboardFocusChange); QWidget *prev = focus_widget; if (prev && reason != Qt::PopupFocusReason && reason != Qt::MenuBarFocusReason) { QInputContext *qic = prev->inputContext(); if(qic) { qic->reset(); qic->setFocusWidget(0); } } focus_widget = focus; if (reason != Qt::NoFocusReason) { //send events if (prev) {#ifdef QT_KEYPAD_NAVIGATION if (QApplication::keypadNavigationEnabled()) { if (prev->hasEditFocus()) prev->setEditFocus(false); }#endif QFocusEvent out(QEvent::FocusOut, reason); QStyle *style = prev->style(); QApplication::sendEvent(prev, &out); QApplication::sendEvent(style, &out); } if(focus && QApplicationPrivate::focus_widget == focus) { QInputContext *qic = focus->inputContext(); if ( qic ) qic->setFocusWidget( focus_widget ); QFocusEvent in(QEvent::FocusIn, reason); QApplication::sendEvent(focus, &in); QApplication::sendEvent(focus->style(), &in); } } emit qApp->focusChanged(prev, focus_widget); }}/*! Returns the application top-level window that has the keyboard input focus, or 0 if no application window has the focus. Note that there might be an activeWindow() even if there is no focusWidget(), for example if no widget in that window accepts key events. \sa QWidget::setFocus(), QWidget::hasFocus(), focusWidget()*/QWidget *QApplication::activeWindow(){ return QApplicationPrivate::active_window;}/*! Returns display (screen) font metrics for the application font. \sa font(), setFont(), QWidget::fontMetrics(), QPainter::fontMetrics()*/QFontMetrics QApplication::fontMetrics(){ return desktop()->fontMetrics();}/*! Closes all top-level windows. This function is particularly useful for applications with many top-level windows. It could, for example, be connected to a \gui{Exit} entry in the \gui{File} menu: \quotefromfile mainwindows/mdi/mainwindow.cpp \skipto exitAct = new QAct \printuntil SLOT(closeAllWindows()) The windows are closed in random order, until one window does not accept the close event. The application quits when the last window was successfully closed; this can be turned off by setting \l quitOnLastWindowClosed to false. \sa quitOnLastWindowClosed, lastWindowClosed() QWidget::close(), QWidget::closeEvent(), lastWindowClosed(), quit(), topLevelWidgets(), QWidget::isWindow()*/void QApplication::closeAllWindows(){ bool did_close = true; QWidget *w; while((w = activeModalWidget()) && did_close) { if(!w->isVisible()) break; did_close = w->close(); } QWidgetList list = QApplication::topLevelWidgets(); for (int i = 0; did_close && i < list.size(); ++i) { w = list.at(i); if (w->isVisible() && w->windowType() != Qt::Desktop) { did_close = w->close(); list = QApplication::topLevelWidgets(); i = -1; } }}/*! Displays a simple message box about Qt. 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. This function is a convenience slot for QMessageBox::aboutQt().*/void QApplication::aboutQt(){#ifndef QT_NO_MESSAGEBOX QMessageBox::aboutQt(activeWindow());#endif // QT_NO_MESSAGEBOX}/*! \fn void QApplication::lastWindowClosed() This signal is emitted from QApplication::exec() when the last visible primary window (i.e. window with no parent) with the Qt::WA_QuitOnClose attribute set is closed. By default, \list \i this attribute is set for all widgets except transient windows such as splash screens, tool windows, and popup menus \i QApplication implicitly quits when this signal is emitted. \endlist This feature be turned off by setting \l quitOnLastWindowClosed to false. \sa QWidget::close()*//*! \since 4.1 \fn void QApplication::focusChanged(QWidget *old, QWidget *now) This signal is emitted when the widget that has keyboard focus changed from \a old to \a now, i.e. because the user pressed the tab-key, clicked into a widget or changed the active window. Note that both \a old and \a now can be the null-pointer. The signal is emitted after both widget have been notified about the change through QFocusEvent. \sa QWidget::setFocus() QWidget::clearFocus() Qt::FocusReason*/#ifndef QT_NO_TRANSLATIONstatic bool qt_detectRTLLanguage(){ return force_reverse ^ QApplication::tr("QT_LAYOUT_DIRECTION", "Translate this string to the string 'LTR' in left-to-right" " languages or to 'RTL' in right-to-left languages (such as Hebrew" " and Arabic) to get proper widget layout.") == QLatin1String("RTL");}#endif/*!\reimp*/bool QApplication::event(QEvent *e){ Q_D(QApplication); if(e->type() == QEvent::Close) { QCloseEvent *ce = static_cast<QCloseEvent*>(e); ce->accept(); closeAllWindows(); QWidgetList list = topLevelWidgets(); for (int i = 0; i < list.size(); ++i) { QWidget *w = list.at(i); if (w->isVisible() && !(w->windowType() == Qt::Desktop) && !(w->windowType() == Qt::Popup) && (!(w->windowType() == Qt::Dialog) || !w->parentWidget())) { ce->ignore(); break; } } if(ce->isAccepted()) return true; } else if(e->type() == QEvent::LanguageChange) {#ifndef QT_NO_TRANSLATION setLayoutDirection(qt_detectRTLLanguage()?Qt::RightToLeft:Qt::LeftToRight);#endif QWidgetList list = topLevelWidgets(); for (int i = 0; i < list.size(); ++i) { QWidget *w = list.at(i); if (!(w->windowType() == Qt::Desktop)) postEvent(w, new QEvent(QEvent::LanguageChange)); } } else if (e->type() == QEvent::Timer) { QTimerEvent *te = static_cast<QTimerEvent*>(e); Q_ASSERT(te != 0); if (te->timerId() == d->toolTipWakeUp.timerId()) { d->toolTipWakeUp.stop(); d->toolTipFallAsleep.start(2000, this); if (d->toolTipWidget) { QHelpEvent e(QEvent::ToolTip, d->toolTipPos, d->toolTipGlobalPos); QApplication::sendEvent(d->toolTipWidget, &e); } } else if (te->timerId() == d->toolTipFallAsleep.timerId()) { d->toolTipFallAsleep.stop(); } } return QCoreApplication::event(e);}#if !defined(Q_WS_X11)// The doc and X implementation of this function is in qapplication_x11.cppvoid QApplication::syncX() {} // do nothing#endif/*! \fn Qt::WindowsVersion QApplication::winVersion() Use \l QSysInfo::WindowsVersion instead.*//*! \fn void QApplication::setActiveWindow(QWidget* active) Sets the active window to the \a active widget in response to a system event. The function is called from the platform specific event handlers. \warning This function does \e not set the keyboard focus to the active widget. Call QWidget::activateWindow() instead. It sets the activeWindow() and focusWidget() attributes and sends proper \l{QEvent::WindowActivate}{WindowActivate}/\l{QEvent::WindowDeactivate}{WindowDeactivate} and \l{QEvent::FocusIn}{FocusIn}/\l{QEvent::FocusOut}{FocusOut} events to all appropriate widgets. The window will then be painted in active state (e.g. cursors in line edits will blink), and it will have tool tips enabled. \sa activeWindow(), QWidget::activateWindow()*/void QApplication::setActiveWindow(QWidget* act){ QWidget* window = act?act->window():0; if (QApplicationPrivate::active_window == window) return; QWidgetList toBeActivated; QWidgetList toBeDeactivated; if (QApplicationPrivate::active_window) { if (style()->styleHint(QStyle::SH_Widget_ShareActivation, 0, QApplicationPrivate::active_window)) { QWidgetList list = topLevelWidgets(); for (int i = 0; i < list.size(); ++i) { QWidget *w = list.at(i); if (w->isVisible() && w->isActiveWindow()) toBeDeactivated.append(w); } } else { toBeDeactivated.append(QApplicationPrivate::active_window); } } QApplicationPrivate::active_window = window; if (QApplicationPrivate::active_window) { if (style()->styleHint(QStyle::SH_Widget_ShareActivation, 0, QApplicationPrivate::active_window)) { QWidgetList list = topLevelWidgets(); for (int i = 0; i < list.size(); ++i) { QWidget *w = list.at(i); if (w->isVisible() && w->isActiveWindow()) toBeActivated.append(w); } } else { toBeActivated.append(QApplicationPrivate::active_window); } } // first the activation/deactivation events QEvent activationChange(QEvent::ActivationChange); QEvent windowActivate(QEvent::WindowActivate); QEvent windowDeactivate(QEvent::WindowDeactivate); for (int i = 0; i < toBeActivated.size(); ++i) { QWidget *w = toBeActivated.at(i); sendSpontaneousEvent(w, &windowActivate); sendSpontaneousEvent(w, &activationChange); } for(int i = 0; i < toBeDeactivated.size(); ++i) { QWidget *w = toBeDeactivated.at(i); sendSpontaneousEvent(w, &windowDeactivate); sendSpontaneousEvent(w, &activationChange); } if (QApplicationPrivate::popupWidgets == 0) { // !inPopupMode() // then focus events if (!QApplicationPrivate::active_window && QApplicationPrivate::focus_widget) { QApplicationPrivate::setFocusWidget(0, Qt::ActiveWindowFocusReason); } else if (QApplicationPrivate::active_window) { QWidget *w = QApplicationPrivate::active_window->focusWidget(); if (w /*&& w->focusPolicy() != QWidget::NoFocus*/) w->setFocus(Qt::ActiveWindowFocusReason); else { QWidget *w = QApplicationPrivate::focusNextPrevChild_helper(QApplicationPrivate::active_window, true); if (w) w->setFocus(Qt::ActiveWindowFocusReason); } } }}/*!internal * Helper function that returns the new focus widget, but does not set the focus reason. * Returns 0 if a new focus widget could not be found.*/QWidget *QApplicationPrivate::focusNextPrevChild_helper(QWidget *toplevel, bool next){ uint focus_flag = qt_tab_all_widgets ? Qt::TabFocus : Qt::StrongFocus; QWidget *f = toplevel->focusWidget(); if (!f) f = toplevel; QWidget *w = f; QWidget *test = f->d_func()->focus_next; while (test && test != f) { if ((test->focusPolicy() & focus_flag) == focus_flag && !(test->d_func()->extra && test->d_func()->extra->focus_proxy) && test->isVisibleTo(toplevel) && test->isEnabled() && (toplevel->windowType() != Qt::SubWindow || toplevel->isAncestorOf(test))) { w = test; if (next) break; } test = test->d_func()->focus_next; } if (w == f) { if (qt_in_tab_key_event) { w->window()->setAttribute(Qt::WA_KeyboardFocusChange); w->update(); } return 0; } return w;}/*!\internal Creates the proper Enter/Leave event when widget \a enter is entered and widget \a leave is left. */void QApplicationPrivate::dispatchEnterLeave(QWidget* enter, QWidget* leave) {#if 0 if (leave) { QEvent e(QEvent::Leave); QApplication::sendEvent(leave, & e); } if (enter) { QEvent e(QEvent::Enter); QApplication::sendEvent(enter, & e); } return;#endif QWidget* w ; if (!enter && !leave) return; QWidgetList leaveList; QWidgetList enterList; bool sameWindow = leave && enter && leave->window() == enter->window(); if (leave && !sameWindow) { w = leave; do { leaveList.append(w); } while (!w->isWindow() && (w = w->parentWidget())); } if (enter && !sameWindow) { w = enter;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -