📄 qapplication.cpp
字号:
} } if (!app_style) qFatal("No styles available!"); } // take ownership of the style QApplicationPrivate::app_style->setParent(qApp); if (!QApplicationPrivate::sys_pal) QApplicationPrivate::setSystemPalette(QApplicationPrivate::app_style->standardPalette()); if (QApplicationPrivate::set_pal) // repolish set palette with the new style QApplication::setPalette(*QApplicationPrivate::set_pal);#ifndef QT_NO_STYLE_STYLESHEET if (!QApplicationPrivate::styleSheet.isEmpty()) { qApp->setStyleSheet(QApplicationPrivate::styleSheet); } else#endif QApplicationPrivate::app_style->polish(qApp); return QApplicationPrivate::app_style;}/*! Sets the application's GUI style to \a style. Ownership of the style object is transferred to QApplication, so QApplication will delete the style object on application exit or when a new style is set. Example usage: \code QApplication::setStyle(new QWindowsStyle); \endcode When switching application styles, the color palette is set back to the initial colors or the system defaults. This is necessary since certain styles have to adapt the color palette to be fully style-guide compliant. Note that setting the style before a palette has been set (i.e. before creating QApplication) will cause the application to use QStyle::standardPalette() for the palette. \sa style(), QStyle, setPalette(), desktopSettingsAware()*/void QApplication::setStyle(QStyle *style){ if (!style || style == QApplicationPrivate::app_style) return; QWidgetList all = allWidgets(); // clean up the old style if (QApplicationPrivate::app_style) { if (QApplicationPrivate::is_app_running && !QApplicationPrivate::is_app_closing) { for (QWidgetList::ConstIterator it = all.constBegin(); it != all.constEnd(); ++it) { register QWidget *w = *it; if (!(w->windowType() == Qt::Desktop) && // except desktop w->testAttribute(Qt::WA_WState_Polished)) { // has been polished QApplicationPrivate::app_style->unpolish(w); } } } QApplicationPrivate::app_style->unpolish(qApp); } QStyle *old = QApplicationPrivate::app_style; // save#ifndef QT_NO_STYLE_STYLESHEET if (!QApplicationPrivate::styleSheet.isEmpty() && !qobject_cast<QStyleSheetStyle *>(style)) { // we have a stylesheet already and a new style is being set QStyleSheetStyle *newProxy = new QStyleSheetStyle(style); style->setParent(newProxy); QApplicationPrivate::app_style = newProxy; } else#endif // QT_NO_STYLE_STYLESHEET QApplicationPrivate::app_style = style; QApplicationPrivate::app_style->setParent(qApp); // take ownership // take care of possible palette requirements of certain gui // styles. Do it before polishing the application since the style // might call QApplication::setPalette() itself if (QApplicationPrivate::set_pal) { QApplication::setPalette(*QApplicationPrivate::set_pal); } else if (QApplicationPrivate::sys_pal) { QApplicationPrivate::initializeWidgetPaletteHash(); QApplicationPrivate::setPalette_helper(*QApplicationPrivate::sys_pal, /*className=*/0, /*clearWidgetPaletteHash=*/false); } else if (!QApplicationPrivate::sys_pal) { // Initialize the sys_pal if it hasn't happened yet... QApplicationPrivate::setSystemPalette(QApplicationPrivate::app_style->standardPalette()); } // initialize the application with the new style QApplicationPrivate::app_style->polish(qApp); // re-polish existing widgets if necessary if (QApplicationPrivate::is_app_running && !QApplicationPrivate::is_app_closing) { for (QWidgetList::ConstIterator it1 = all.constBegin(); it1 != all.constEnd(); ++it1) { register QWidget *w = *it1; if (w->windowType() != Qt::Desktop && w->testAttribute(Qt::WA_WState_Polished)) { if (w->style() == QApplicationPrivate::app_style) QApplicationPrivate::app_style->polish(w); // repolish#ifndef QT_NO_STYLE_STYLESHEET else w->setStyleSheet(w->styleSheet()); // touch#endif } } for (QWidgetList::ConstIterator it2 = all.constBegin(); it2 != all.constEnd(); ++it2) { register QWidget *w = *it2; if (w->windowType() != Qt::Desktop && !w->testAttribute(Qt::WA_SetStyle)) { QEvent e(QEvent::StyleChange); QApplication::sendEvent(w, &e);#ifdef QT3_SUPPORT if (old) w->styleChange(*old);#endif w->update(); } } }#ifndef QT_NO_STYLE_STYLESHEET if (QStyleSheetStyle *oldProxy = qobject_cast<QStyleSheetStyle *>(old)) { oldProxy->deref(); } else#endif if (old && old->parent() == qApp) { delete old; } if (QApplicationPrivate::focus_widget) { QFocusEvent in(QEvent::FocusIn, Qt::OtherFocusReason); QApplication::sendEvent(QApplicationPrivate::focus_widget->style(), &in); QApplicationPrivate::focus_widget->update(); }}/*! \overload Requests a QStyle object for \a style from the QStyleFactory. The string must be one of the QStyleFactory::keys(), typically one of "windows", "motif", "cde", "plastique", "windowsxp", or "macintosh". Style names are case insensitive. Returns 0 if an unknown \a style is passed, otherwise the QStyle object returned is set as the application's GUI style. \warning To ensure that the application's style is set correctly, it is best to call this function before the QApplication constructor, if possible.*/QStyle* QApplication::setStyle(const QString& style){ QStyle *s = QStyleFactory::create(style); if (!s) return 0; setStyle(s); return s;}/*! Returns the color specification. \sa QApplication::setColorSpec() */int QApplication::colorSpec(){ return QApplicationPrivate::app_cspec;}/*! Sets the color specification for the application to \a spec. The color specification controls how the application allocates colors when run on a display with a limited amount of colors, e.g. 8 bit / 256 color displays. The color specification must be set before you create the QApplication object. The options are: \list \o QApplication::NormalColor. This is the default color allocation strategy. Use this option if your application uses buttons, menus, texts and pixmaps with few colors. With this option, the application uses system global colors. This works fine for most applications under X11, but on Windows machines it may cause dithering of non-standard colors. \o QApplication::CustomColor. Use this option if your application needs a small number of custom colors. On X11, this option is the same as NormalColor. On Windows, Qt creates a Windows palette, and allocates colors to it on demand. \o QApplication::ManyColor. Use this option if your application is very color hungry (e.g. it requires thousands of colors). Under X11 the effect is: \list \o For 256-color displays which have at best a 256 color true color visual, the default visual is used, and colors are allocated from a color cube. The color cube is the 6x6x6 (216 color) "Web palette" (the red, green, and blue components always have one of the following values: 0x00, 0x33, 0x66, 0x99, 0xCC, or 0xFF), but the number of colors can be changed by the \e -ncols option. The user can force the application to use the true color visual with the \link QApplication::QApplication() -visual \endlink option. \o For 256-color displays which have a true color visual with more than 256 colors, use that visual. Silicon Graphics X servers have this feature, for example. They provide an 8 bit visual by default but can deliver true color when asked. \endlist On Windows, Qt creates a Windows palette, and fills it with a color cube. \endlist Be aware that the CustomColor and ManyColor choices may lead to colormap flashing: The foreground application gets (most) of the available colors, while the background windows will look less attractive. Example: \code int main(int argc, char *argv[]) { QApplication::setColorSpec(QApplication::ManyColor); QApplication app(argc, argv); ... return app.exec(); } \endcode \sa colorSpec()*/void QApplication::setColorSpec(int spec){ if (qApp) qWarning("QApplication::setColorSpec: This function must be " "called before the QApplication object is created"); QApplicationPrivate::app_cspec = spec;}/*! \property QApplication::globalStrut \brief the minimum size that any GUI element that the user can interact with should have For example no button should be resized to be smaller than the global strut size. The strut size should be considered when reimplementing GUI controls that may be used on touch-screens or similar I/O devices. Example: \code QSize MyWidget::sizeHint() const { return QSize(80, 25).expandedTo(QApplication::globalStrut()); } \endcode*/QSize QApplication::globalStrut(){ return QApplicationPrivate::app_strut;}void QApplication::setGlobalStrut(const QSize& strut){ QApplicationPrivate::app_strut = strut;}/*! Returns the application palette. \sa setPalette(), QWidget::palette()*/QPalette QApplication::palette(){ if (!QApplicationPrivate::app_pal) QApplicationPrivate::app_pal = new QPalette(Qt::black); return *QApplicationPrivate::app_pal;}/*! \fn QPalette QApplication::palette(const QWidget* widget) \overload If a \a widget is passed, the default palette for the widget's class is returned. This may or may not be the application palette. In most cases there isn't a special palette for certain types of widgets, but one notable exception is the popup menu under Windows, if the user has defined a special background color for menus in the display settings. \sa setPalette(), QWidget::palette()*/QPalette QApplication::palette(const QWidget* w){ PaletteHash *hash = app_palettes(); if (w && hash && hash->size()) { QHash<QByteArray, QPalette>::ConstIterator it = hash->constFind(w->metaObject()->className()); if (it != hash->constEnd()) return *it; for (it = hash->constBegin(); it != hash->constEnd(); ++it) { if (w->inherits(it.key())) return it.value(); } } return palette();}/*! \overload Returns the palette for widgets of the given \a className. \sa setPalette(), QWidget::palette()*/QPalette QApplication::palette(const char *className){ if (!QApplicationPrivate::app_pal) palette(); PaletteHash *hash = app_palettes(); if (className && hash && hash->size()) { QHash<QByteArray, QPalette>::ConstIterator it = hash->constFind(className); if (it != hash->constEnd()) return *it; } return *QApplicationPrivate::app_pal;}void QApplicationPrivate::setPalette_helper(const QPalette &palette, const char* className, bool clearWidgetPaletteHash){ QPalette pal = palette; if (QApplicationPrivate::app_style) QApplicationPrivate::app_style->polish(pal); // NB: non-const reference bool all = false; PaletteHash *hash = app_palettes(); if (!className) { if (QApplicationPrivate::app_pal && pal.isCopyOf(*QApplicationPrivate::app_pal)) return; if (!QApplicationPrivate::app_pal) QApplicationPrivate::app_pal = new QPalette(pal); else *QApplicationPrivate::app_pal = pal; if (hash && hash->size()) { all = true; if (clearWidgetPaletteHash) hash->clear(); } } else if (hash) { hash->insert(className, pal); } if (QApplicationPrivate::is_app_running && !QApplicationPrivate::is_app_closing) { QEvent e(QEvent::ApplicationPaletteChange); 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 QApplication::sendEvent(w, &e); } } if (!className && (!QApplicationPrivate::sys_pal || !palette.isCopyOf(*QApplicationPrivate::sys_pal))) { if (!QApplicationPrivate::set_pal) QApplicationPrivate::set_pal = new QPalette(palette); else *QApplicationPrivate::set_pal = palette; }}/*! Changes the default application palette to \a palette. If \a className is passed, the change applies only to widgets that inherit \a className (as reported by QObject::inherits()). If \a className is left 0, the change affects all widgets, thus overriding any previously set class specific palettes. The palette may be changed according to the current GUI style in QStyle::polish(). \warning Do not use this function in conjunction with \l{Qt Style Sheets}. When using style sheets, the palette of a widget can be customized using the "color", "background-color", "selection-color", "selection-background-color" and "alternate-background-color". \sa QWidget::setPalette(), palette(), QStyle::polish()*/void QApplication::setPalette(const QPalette &palette, const char* className)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -