📄 qapplication.cpp
字号:
{ QApplicationPrivate::setPalette_helper(palette, className, /*clearWidgetPaletteHash=*/ true);}void QApplicationPrivate::setSystemPalette(const QPalette &pal){ QPalette adjusted;#if 0 // adjust the system palette to avoid dithering QColormap cmap = QColormap::instance(); if (cmap.depths() > 4 && cmap.depths() < 24) { for (int g = 0; g < QPalette::NColorGroups; g++) for (int i = 0; i < QPalette::NColorRoles; i++) { QColor color = pal.color((QPalette::ColorGroup)g, (QPalette::ColorRole)i); color = cmap.colorAt(cmap.pixel(color)); adjusted.setColor((QPalette::ColorGroup)g, (QPalette::ColorRole) i, color); } }#else adjusted = pal;#endif if (!sys_pal) sys_pal = new QPalette(adjusted); else *sys_pal = adjusted; if (!QApplicationPrivate::set_pal) QApplication::setPalette(*sys_pal);}/*! Returns the default application font. \sa fontMetrics(), QWidget::font()*/QFont QApplication::font(){ if (!QApplicationPrivate::app_font) QApplicationPrivate::app_font = new QFont(QLatin1String("Helvetica")); return *QApplicationPrivate::app_font;}/*! \overload Returns the default font for the \a widget. \sa fontMetrics(), QWidget::setFont()*/QFont QApplication::font(const QWidget *widget){ FontHash *hash = app_fonts(); if (widget && hash && hash->size()) { QHash<QByteArray, QFont>::ConstIterator it = hash->constFind(widget->metaObject()->className()); if (it != hash->constEnd()) return it.value(); for (it = hash->constBegin(); it != hash->constEnd(); ++it) { if (widget->inherits(it.key())) return it.value(); } } return font();}/*! \overload Returns the font for widgets of the given \a className. \sa setFont(), QWidget::font()*/QFont QApplication::font(const char *className){ FontHash *hash = app_fonts(); if (className && hash && hash->size()) { QHash<QByteArray, QFont>::ConstIterator it = hash->constFind(className); if (it != hash->constEnd()) return *it; } return font();}/*! Changes the default application font to \a font. If \a className is passed, the change applies only to classes that inherit \a className (as reported by QObject::inherits()). On application start-up, the default font depends on the window system. It can vary depending on both the window system version and the locale. This function lets you override the default font; but overriding may be a bad idea because, for example, some locales need extra large fonts to support their special characters. \warning Do not use this function in conjunction with \l{Qt Style Sheets}. The font of an application can be customized using the "font" style sheet property. To set a bold font for all QPushButtons, set the application styleSheet() as "QPushButton { font: bold }" \sa font(), fontMetrics(), QWidget::setFont()*/void QApplication::setFont(const QFont &font, const char *className){ bool all = false; FontHash *hash = app_fonts(); if (!className) { if (!QApplicationPrivate::app_font) QApplicationPrivate::app_font = new QFont(font); else *QApplicationPrivate::app_font = font; if (hash && hash->size()) { all = true; hash->clear(); } } else if (hash) { hash->insert(className, font); } if (QApplicationPrivate::is_app_running && !QApplicationPrivate::is_app_closing) { QEvent e(QEvent::ApplicationFontChange); QWidgetList wids = QApplication::allWidgets(); for (QWidgetList::ConstIterator it = wids.constBegin(); it != wids.constEnd(); ++it) { register QWidget *w = *it; if (all || (!className && w->isWindow()) || w->inherits(className)) // matching class sendEvent(w, &e); } } if (!className && (!QApplicationPrivate::sys_font || !font.isCopyOf(*QApplicationPrivate::sys_font))) { if (!QApplicationPrivate::set_font) QApplicationPrivate::set_font = new QFont(font); else *QApplicationPrivate::set_font = font; }}/*! \internal*/void QApplicationPrivate::setSystemFont(const QFont &font){ if (!sys_font) sys_font = new QFont(font); else *sys_font = font; if (!QApplicationPrivate::set_font) QApplication::setFont(*sys_font);}/*! \property QApplication::windowIcon \brief the default window icon \sa QWidget::setWindowIcon(), {Setting the Application Icon}*/QIcon QApplication::windowIcon(){ return QApplicationPrivate::app_icon ? *QApplicationPrivate::app_icon : QIcon();}void QApplication::setWindowIcon(const QIcon &icon){ if (!QApplicationPrivate::app_icon) QApplicationPrivate::app_icon = new QIcon(); *QApplicationPrivate::app_icon = icon; if (QApplicationPrivate::is_app_running && !QApplicationPrivate::is_app_closing) {#ifdef Q_WS_MAC void qt_mac_set_app_icon(const QPixmap &); //qapplication_mac.cpp QSize size = QApplicationPrivate::app_icon->actualSize(QSize(128, 128)); qt_mac_set_app_icon(QApplicationPrivate::app_icon->pixmap(size));#endif QEvent e(QEvent::ApplicationWindowIconChange); QWidgetList all = QApplication::allWidgets(); for (QWidgetList::ConstIterator it = all.constBegin(); it != all.constEnd(); ++it) { register QWidget *w = *it; if (w->isWindow()) sendEvent(w, &e); } }}/*! Returns a list of the top-level widgets (windows) in the application. Note that some of the top-level widgets may be hidden, for example a tooltip if no tooltip is currently shown. Example: \code void showAllHiddenTopLevelWidgets() { foreach (QWidget *widget, QApplication::topLevelWidgets()) { if (widget->isHidden()) widget->show(); } } \endcode \sa allWidgets(), QWidget::isWindow(), QWidget::isHidden()*/QWidgetList QApplication::topLevelWidgets(){ QWidgetList list; QWidgetList all = allWidgets(); for (QWidgetList::ConstIterator it = all.constBegin(); it != all.constEnd(); ++it) { QWidget *w = *it; if (w->isWindow() && w->windowType() != Qt::Desktop) list.append(w); } return list;}/*! Returns a list of all the widgets in the application. The list is empty (QList::isEmpty()) if there are no widgets. Note that some of the widgets may be hidden. Example: \code void updateAllWidgets() { foreach (QWidget *widget, QApplication::allWidgets()) widget->update(); } \endcode \sa topLevelWidgets(), QWidget::isVisible()*/QWidgetList QApplication::allWidgets(){ QWidgetList list; if (QWidgetPrivate::mapper) list += QWidgetPrivate::mapper->values(); if (QWidgetPrivate::uncreatedWidgets) list += QWidgetPrivate::uncreatedWidgets->toList(); return list;}/*! Returns the application widget that has the keyboard input focus, or 0 if no widget in this application has the focus. \sa QWidget::setFocus(), QWidget::hasFocus(), activeWindow(), focusChanged()*/QWidget *QApplication::focusWidget(){ return QApplicationPrivate::focus_widget;}void QApplicationPrivate::setFocusWidget(QWidget *focus, Qt::FocusReason reason){ hidden_focus_widget = 0; if (focus != focus_widget) { if (focus && focus->isHidden()) { hidden_focus_widget = focus; return; } if (focus && (reason == Qt::BacktabFocusReason || reason == Qt::TabFocusReason) && qt_in_tab_key_event) focus->window()->setAttribute(Qt::WA_KeyboardFocusChange); else if (focus && reason == Qt::ShortcutFocusReason) { 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(focus_widget) focus_widget->d_func()->setFocus_sys(); if (reason != Qt::NoFocusReason) { //send events if (prev) {#ifdef QT_KEYPAD_NAVIGATION if (QApplication::keypadNavigationEnabled()) { if (prev->hasEditFocus() && reason != Qt::PopupFocusReason) prev->setEditFocus(false); }#endif QFocusEvent out(QEvent::FocusOut, reason); QPointer<QWidget> that = prev; QApplication::sendEvent(prev, &out); if (that) QApplication::sendEvent(that->style(), &out); } if(focus && QApplicationPrivate::focus_widget == focus) { QInputContext *qic = focus->inputContext(); if (qic && focus_widget->testAttribute(Qt::WA_WState_Created)) qic->setFocusWidget( focus_widget ); QFocusEvent in(QEvent::FocusIn, reason); QPointer<QWidget> that = focus; QApplication::sendEvent(focus, &in); if (that) QApplication::sendEvent(that->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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -