diff options
-rw-r--r-- | examples/widgets/widgets/styles/widgetgallery.cpp | 8 | ||||
-rw-r--r-- | examples/widgets/widgets/styles/widgetgallery.h | 2 | ||||
-rw-r--r-- | src/gui/kernel/qguiapplication.cpp | 117 | ||||
-rw-r--r-- | src/gui/kernel/qguiapplication_p.h | 11 | ||||
-rw-r--r-- | src/plugins/styles/mac/qmacstyle_mac.mm | 2 | ||||
-rw-r--r-- | src/plugins/styles/windowsvista/qwindowsxpstyle.cpp | 3 | ||||
-rw-r--r-- | src/widgets/kernel/qapplication.cpp | 231 | ||||
-rw-r--r-- | src/widgets/kernel/qapplication_p.h | 10 | ||||
-rw-r--r-- | tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp | 51 |
9 files changed, 227 insertions, 208 deletions
diff --git a/examples/widgets/widgets/styles/widgetgallery.cpp b/examples/widgets/widgets/styles/widgetgallery.cpp index d70c503f53..bf8e93feb4 100644 --- a/examples/widgets/widgets/styles/widgetgallery.cpp +++ b/examples/widgets/widgets/styles/widgetgallery.cpp @@ -75,8 +75,6 @@ WidgetGallery::WidgetGallery(QWidget *parent) : QDialog(parent) { - originalPalette = QApplication::palette(); - styleComboBox = new QComboBox; const QString defaultStyleName = QApplication::style()->objectName(); QStringList styleNames = QStyleFactory::keys(); @@ -162,10 +160,8 @@ void WidgetGallery::changeStyle(const QString &styleName) void WidgetGallery::changePalette() //! [7] //! [8] { - if (useStylePaletteCheckBox->isChecked()) - QApplication::setPalette(QApplication::style()->standardPalette()); - else - QApplication::setPalette(originalPalette); + QApplication::setPalette(useStylePaletteCheckBox->isChecked() ? + QApplication::style()->standardPalette() : QPalette()); } //! [8] diff --git a/examples/widgets/widgets/styles/widgetgallery.h b/examples/widgets/widgets/styles/widgetgallery.h index 4deee6839a..8c4c880536 100644 --- a/examples/widgets/widgets/styles/widgetgallery.h +++ b/examples/widgets/widgets/styles/widgetgallery.h @@ -96,8 +96,6 @@ private: void createBottomRightGroupBox(); void createProgressBar(); - QPalette originalPalette; - QLabel *styleLabel; QComboBox *styleComboBox; QCheckBox *useStylePaletteCheckBox; diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index ffd5b04276..445ad6b835 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -236,21 +236,6 @@ static bool qt_detectRTLLanguage() " and Arabic) to get proper widget layout.") == QLatin1String("RTL")); } -static void initPalette() -{ - if (!QGuiApplicationPrivate::app_pal) - if (const QPalette *themePalette = QGuiApplicationPrivate::platformTheme()->palette()) - QGuiApplicationPrivate::app_pal = new QPalette(*themePalette); - if (!QGuiApplicationPrivate::app_pal) - QGuiApplicationPrivate::app_pal = new QPalette(Qt::gray); -} - -static inline void clearPalette() -{ - delete QGuiApplicationPrivate::app_pal; - QGuiApplicationPrivate::app_pal = nullptr; -} - static void initFontUnlocked() { if (!QGuiApplicationPrivate::app_font) { @@ -691,7 +676,7 @@ QGuiApplication::~QGuiApplication() d->session_manager = nullptr; #endif //QT_NO_SESSIONMANAGER - clearPalette(); + QGuiApplicationPrivate::clearPalette(); QFontDatabase::removeAllApplicationFonts(); #ifndef QT_NO_CURSOR @@ -1611,7 +1596,7 @@ void QGuiApplicationPrivate::init() if (platform_integration == nullptr) createPlatformIntegration(); - initPalette(); + updatePalette(); QFont::initialize(); initThemeHints(); @@ -3289,46 +3274,97 @@ QClipboard * QGuiApplication::clipboard() */ /*! - Returns the default application palette. + Returns the current application palette. + + Roles that have not been explicitly set will reflect the system's platform theme. \sa setPalette() */ QPalette QGuiApplication::palette() { - initPalette(); + if (!QGuiApplicationPrivate::app_pal) + QGuiApplicationPrivate::updatePalette(); + return *QGuiApplicationPrivate::app_pal; } +void QGuiApplicationPrivate::updatePalette() +{ + if (app_pal) { + if (setPalette(*app_pal) && qGuiApp) + qGuiApp->d_func()->handlePaletteChanged(); + } else { + setPalette(QPalette()); + } +} + +void QGuiApplicationPrivate::clearPalette() +{ + delete app_pal; + app_pal = nullptr; +} + /*! - Changes the default application palette to \a pal. + Changes the application palette to \a pal. + + The color roles from this palette are combined with the system's platform + theme to form the application's final palette. \sa palette() */ void QGuiApplication::setPalette(const QPalette &pal) { - if (!QGuiApplicationPrivate::setPalette(pal)) - return; - - QCoreApplication::setAttribute(Qt::AA_SetPalette); - - if (qGuiApp) - qGuiApp->d_func()->sendApplicationPaletteChange(); + if (QGuiApplicationPrivate::setPalette(pal) && qGuiApp) + qGuiApp->d_func()->handlePaletteChanged(); } bool QGuiApplicationPrivate::setPalette(const QPalette &palette) { - if (app_pal && palette.isCopyOf(*app_pal)) + // Resolve the palette against the theme palette, filling in + // any missing roles, while keeping the original resolve mask. + QPalette basePalette = qGuiApp ? qGuiApp->d_func()->basePalette() : Qt::gray; + basePalette.resolve(0); // The base palette only contributes missing colors roles + QPalette resolvedPalette = palette.resolve(basePalette); + + if (app_pal && resolvedPalette == *app_pal && resolvedPalette.resolve() == app_pal->resolve()) return false; if (!app_pal) - app_pal = new QPalette(palette); + app_pal = new QPalette(resolvedPalette); else - *app_pal = palette; + *app_pal = resolvedPalette; + + QCoreApplication::setAttribute(Qt::AA_SetPalette, app_pal->resolve() != 0); return true; } +/* + Returns the base palette used to fill in missing roles in + the current application palette. + + Normally this is the theme palette, but QApplication + overrides this for compatibility reasons. +*/ +QPalette QGuiApplicationPrivate::basePalette() const +{ + return platformTheme() ? *platformTheme()->palette() : Qt::gray; +} + +void QGuiApplicationPrivate::handlePaletteChanged(const char *className) +{ + if (!className) { + Q_ASSERT(app_pal); + emit qGuiApp->paletteChanged(*QGuiApplicationPrivate::app_pal); + } + + if (is_app_running && !is_app_closing) { + QEvent event(QEvent::ApplicationPaletteChange); + QGuiApplication::sendEvent(qGuiApp, &event); + } +} + void QGuiApplicationPrivate::applyWindowGeometrySpecificationTo(QWindow *window) { windowGeometrySpecification.applyTo(window); @@ -4127,11 +4163,8 @@ QPixmap QGuiApplicationPrivate::getPixmapCursor(Qt::CursorShape cshape) void QGuiApplicationPrivate::notifyThemeChanged() { - if (!testAttribute(Qt::AA_SetPalette)) { - clearPalette(); - initPalette(); - sendApplicationPaletteChange(); - } + updatePalette(); + if (!(applicationResourceFlags & ApplicationFontExplicitlySet)) { const auto locker = qt_scoped_lock(applicationFontMutex); clearFontUnlocked(); @@ -4140,20 +4173,6 @@ void QGuiApplicationPrivate::notifyThemeChanged() initThemeHints(); } -void QGuiApplicationPrivate::sendApplicationPaletteChange(bool toAllWidgets, const char *className) -{ - Q_UNUSED(toAllWidgets) - - if (!className) - emit qGuiApp->paletteChanged(*QGuiApplicationPrivate::app_pal); - - if (!is_app_running || is_app_closing) - return; - - QEvent event(QEvent::ApplicationPaletteChange); - QGuiApplication::sendEvent(QGuiApplication::instance(), &event); -} - #if QT_CONFIG(draganddrop) void QGuiApplicationPrivate::notifyDragStarted(const QDrag *drag) { diff --git a/src/gui/kernel/qguiapplication_p.h b/src/gui/kernel/qguiapplication_p.h index 2fe4ae02cf..5239b58c97 100644 --- a/src/gui/kernel/qguiapplication_p.h +++ b/src/gui/kernel/qguiapplication_p.h @@ -323,17 +323,22 @@ public: static void resetCachedDevicePixelRatio(); - static bool setPalette(const QPalette &palette); - protected: virtual void notifyThemeChanged(); - virtual void sendApplicationPaletteChange(bool toAllWidgets = false, const char *className = nullptr); + + static bool setPalette(const QPalette &palette); + virtual QPalette basePalette() const; + virtual void handlePaletteChanged(const char *className = nullptr); + bool tryCloseRemainingWindows(QWindowList processedWindows); #if QT_CONFIG(draganddrop) virtual void notifyDragStarted(const QDrag *); #endif // QT_CONFIG(draganddrop) private: + static void clearPalette(); + static void updatePalette(); + friend class QDragManager; static QGuiApplicationPrivate *self; diff --git a/src/plugins/styles/mac/qmacstyle_mac.mm b/src/plugins/styles/mac/qmacstyle_mac.mm index 5aa7befb84..37b658dd76 100644 --- a/src/plugins/styles/mac/qmacstyle_mac.mm +++ b/src/plugins/styles/mac/qmacstyle_mac.mm @@ -2585,7 +2585,7 @@ QPalette QMacStyle::standardPalette() const auto platformTheme = QGuiApplicationPrivate::platformTheme(); auto styleNames = platformTheme->themeHint(QPlatformTheme::StyleNames); if (styleNames.toStringList().contains("macintosh")) - return *platformTheme->palette(); + return QPalette(); // Inherit everything from theme else return QStyle::standardPalette(); } diff --git a/src/plugins/styles/windowsvista/qwindowsxpstyle.cpp b/src/plugins/styles/windowsvista/qwindowsxpstyle.cpp index bf80138b32..5d2e8efd68 100644 --- a/src/plugins/styles/windowsvista/qwindowsxpstyle.cpp +++ b/src/plugins/styles/windowsvista/qwindowsxpstyle.cpp @@ -3774,8 +3774,7 @@ int QWindowsXPStyle::styleHint(StyleHint hint, const QStyleOption *option, const /*! \reimp */ QPalette QWindowsXPStyle::standardPalette() const { - return QWindowsXPStylePrivate::useXP() && QApplicationPrivate::sys_pal - ? *QApplicationPrivate::sys_pal : QWindowsStyle::standardPalette(); + return QWindowsXPStylePrivate::useXP() ? QPalette() : QWindowsStyle::standardPalette(); } /*! diff --git a/src/widgets/kernel/qapplication.cpp b/src/widgets/kernel/qapplication.cpp index 4b1daab4cf..eea97b2c0b 100644 --- a/src/widgets/kernel/qapplication.cpp +++ b/src/widgets/kernel/qapplication.cpp @@ -140,30 +140,6 @@ Q_GUI_EXPORT bool qt_sendShortcutOverrideEvent(QObject *o, ulong timestamp, int QApplicationPrivate *QApplicationPrivate::self = nullptr; -static void initSystemPalette() -{ - if (QApplicationPrivate::sys_pal) - return; // Already initialized - - QPalette defaultPalette; - if (QApplicationPrivate::app_style) - defaultPalette = QApplicationPrivate::app_style->standardPalette(); - - auto *platformTheme = QGuiApplicationPrivate::platformTheme(); - if (const QPalette *themePalette = platformTheme ? platformTheme->palette() : nullptr) { - QApplicationPrivate::setSystemPalette(themePalette->resolve(defaultPalette)); - QApplicationPrivate::initializeWidgetPaletteHash(); - } else { - QApplicationPrivate::setSystemPalette(defaultPalette); - } -} - -static void clearSystemPalette() -{ - delete QApplicationPrivate::sys_pal; - QApplicationPrivate::sys_pal = nullptr; -} - bool QApplicationPrivate::autoSipEnabled = true; QApplicationPrivate::QApplicationPrivate(int &argc, char **argv, int flags) @@ -381,8 +357,6 @@ QString QApplicationPrivate::styleSheet; // default application styles #endif QPointer<QWidget> QApplicationPrivate::leaveAfterRelease = nullptr; -QPalette *QApplicationPrivate::sys_pal = nullptr; // default system palette - QFont *QApplicationPrivate::sys_font = nullptr; // default system font QFont *QApplicationPrivate::set_font = nullptr; // default font set by programmer @@ -545,12 +519,7 @@ void QApplicationPrivate::init() // Must be called before initialize() QColormap::initialize(); - if (sys_pal) { - // Now that we have a platform theme we need to reset - // the system palette to pick up the theme colors. - clearSystemPalette(); - initSystemPalette(); - } + initializeWidgetPalettesFromTheme(); qt_init_tooltip_palette(); QApplicationPrivate::initializeWidgetFontHash(); @@ -629,38 +598,6 @@ void QApplicationPrivate::initialize() is_app_running = true; // no longer starting up } -static void setPossiblePalette(const QPalette *palette, const char *className) -{ - if (palette == nullptr) - return; - QApplicationPrivate::setPalette_helper(*palette, className); -} - -void QApplicationPrivate::initializeWidgetPaletteHash() -{ - QPlatformTheme *platformTheme = QGuiApplicationPrivate::platformTheme(); - if (!platformTheme) - return; - - widgetPalettes.clear(); - - setPossiblePalette(platformTheme->palette(QPlatformTheme::ToolButtonPalette), "QToolButton"); - setPossiblePalette(platformTheme->palette(QPlatformTheme::ButtonPalette), "QAbstractButton"); - setPossiblePalette(platformTheme->palette(QPlatformTheme::CheckBoxPalette), "QCheckBox"); - setPossiblePalette(platformTheme->palette(QPlatformTheme::RadioButtonPalette), "QRadioButton"); - setPossiblePalette(platformTheme->palette(QPlatformTheme::HeaderPalette), "QHeaderView"); - setPossiblePalette(platformTheme->palette(QPlatformTheme::ItemViewPalette), "QAbstractItemView"); - setPossiblePalette(platformTheme->palette(QPlatformTheme::MessageBoxLabelPalette), "QMessageBoxLabel"); - setPossiblePalette(platformTheme->palette(QPlatformTheme::TabBarPalette), "QTabBar"); - setPossiblePalette(platformTheme->palette(QPlatformTheme::LabelPalette), "QLabel"); - setPossiblePalette(platformTheme->palette(QPlatformTheme::GroupBoxPalette), "QGroupBox"); - setPossiblePalette(platformTheme->palette(QPlatformTheme::MenuPalette), "QMenu"); - setPossiblePalette(platformTheme->palette(QPlatformTheme::MenuBarPalette), "QMenuBar"); - setPossiblePalette(platformTheme->palette(QPlatformTheme::TextEditPalette), "QTextEdit"); - setPossiblePalette(platformTheme->palette(QPlatformTheme::TextEditPalette), "QTextControl"); - setPossiblePalette(platformTheme->palette(QPlatformTheme::TextLineEditPalette), "QLineEdit"); -} - void QApplicationPrivate::initializeWidgetFontHash() { const QPlatformTheme *theme = QGuiApplicationPrivate::platformTheme(); @@ -798,9 +735,6 @@ QApplication::~QApplication() delete qt_desktopWidget; qt_desktopWidget = nullptr; - delete QApplicationPrivate::app_pal; - QApplicationPrivate::app_pal = nullptr; - clearSystemPalette(); QApplicationPrivate::widgetPalettes.clear(); delete QApplicationPrivate::sys_font; @@ -1051,10 +985,10 @@ QStyle *QApplication::style() // Take ownership of the style defaultStyle->setParent(qApp); - initSystemPalette(); - if (testAttribute(Qt::AA_SetPalette)) defaultStyle->polish(*QGuiApplicationPrivate::app_pal); + else + QApplicationPrivate::initializeWidgetPalettesFromTheme(); #ifndef QT_NO_STYLE_STYLESHEET if (!QApplicationPrivate::styleSheet.isEmpty()) { @@ -1128,13 +1062,10 @@ void QApplication::setStyle(QStyle *style) // 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 (testAttribute(Qt::AA_SetPalette)) { + if (testAttribute(Qt::AA_SetPalette)) QApplicationPrivate::app_style->polish(*QGuiApplicationPrivate::app_pal); - } else { - if (QApplicationPrivate::sys_pal) - clearSystemPalette(); - initSystemPalette(); - } + else + QApplicationPrivate::initializeWidgetPalettesFromTheme(); // The default widget font hash is based on the platform theme, // not the style, but the widget fonts could in theory have been @@ -1317,6 +1248,22 @@ void QApplication::setGlobalStrut(const QSize& strut) // Widget specific palettes QApplicationPrivate::PaletteHash QApplicationPrivate::widgetPalettes; +QPalette QApplicationPrivate::basePalette() const +{ + // Start out with a palette based on the style, in case there's no theme + // available, or so that we can fill in missing roles in the theme. + QPalette palette = app_style ? app_style->standardPalette() : Qt::gray; + + // Prefer theme palette if available, but fill in missing roles from style + // for compatibility. Note that the style's standard palette is not prioritized + // over the theme palette, as the documented way of applying the style's palette + // is to set it explicitly using QApplication::setPalette(). + if (const QPalette *themePalette = platformTheme() ? platformTheme()->palette() : nullptr) + palette = themePalette->resolve(palette); + + return palette; +} + /*! \fn QPalette QApplication::palette(const QWidget* widget) @@ -1363,35 +1310,8 @@ QPalette QApplication::palette(const char *className) return QGuiApplication::palette(); } -void QApplicationPrivate::setPalette_helper(const QPalette &palette, const char* className) -{ - QPalette pal = palette; - - if (QApplicationPrivate::app_style) - QApplicationPrivate::app_style->polish(pal); // NB: non-const reference - - bool all = false; - if (!className) { - if (!QGuiApplicationPrivate::setPalette(pal)) - return; - - if (!QApplicationPrivate::sys_pal || !palette.isCopyOf(*QApplicationPrivate::sys_pal)) - QCoreApplication::setAttribute(Qt::AA_SetPalette); - - if (!widgetPalettes.isEmpty()) { - all = true; - widgetPalettes.clear(); - } - } else { - widgetPalettes.insert(className, pal); - } - - if (qApp) - qApp->d_func()->sendApplicationPaletteChange(all, className); -} - /*! - Changes the default application palette to \a palette. + Changes the 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 @@ -1412,23 +1332,87 @@ void QApplicationPrivate::setPalette_helper(const QPalette &palette, const char* \sa QWidget::setPalette(), palette(), QStyle::polish() */ - void QApplication::setPalette(const QPalette &palette, const char* className) { - QApplicationPrivate::setPalette_helper(palette, className); + QPalette polishedPalette = palette; + + if (QApplicationPrivate::app_style) + QApplicationPrivate::app_style->polish(polishedPalette); + + if (className) { + QApplicationPrivate::widgetPalettes.insert(className, polishedPalette); + if (qApp) + qApp->d_func()->handlePaletteChanged(className); + } else { + QGuiApplication::setPalette(polishedPalette); + } } +void QApplicationPrivate::handlePaletteChanged(const char *className) +{ + if (!is_app_running || is_app_closing) + return; + + // Setting the global application palette is documented to + // reset any previously set class specific widget palettes. + bool sendPaletteChangeToAllWidgets = false; + if (!className && !widgetPalettes.isEmpty()) { + sendPaletteChangeToAllWidgets = true; + widgetPalettes.clear(); + } + QGuiApplicationPrivate::handlePaletteChanged(className); -void QApplicationPrivate::setSystemPalette(const QPalette &pal) + QEvent event(QEvent::ApplicationPaletteChange); + const QWidgetList widgets = QApplication::allWidgets(); + for (auto widget : widgets) { + if (sendPaletteChangeToAllWidgets || (!className && widget->isWindow()) || (className && widget->inherits(className))) + QCoreApplication::sendEvent(widget, &event); + } + +#if QT_CONFIG(graphicsview) + for (auto scene : qAsConst(scene_list)) + QCoreApplication::sendEvent(scene, &event); +#endif + + // Palette has been reset back to the default application palette, + // so we need to reinitialize the widget palettes from the theme. + if (!className && !testAttribute(Qt::AA_SetPalette)) + initializeWidgetPalettesFromTheme(); +} + +void QApplicationPrivate::initializeWidgetPalettesFromTheme() { - if (!sys_pal) - sys_pal = new QPalette(pal); - else - *sys_pal = pal; + QPlatformTheme *platformTheme = QGuiApplicationPrivate::platformTheme(); + if (!platformTheme) + return; - if (!testAttribute(Qt::AA_SetPalette)) - QApplication::setPalette(*sys_pal); + widgetPalettes.clear(); + + struct ThemedWidget { const char *className; QPlatformTheme::Palette palette; }; + + static const ThemedWidget themedWidgets[] = { + { "QToolButton", QPlatformTheme::ToolButtonPalette }, + { "QAbstractButton", QPlatformTheme::ButtonPalette }, + { "QCheckBox", QPlatformTheme::CheckBoxPalette }, + { "QRadioButton", QPlatformTheme::RadioButtonPalette }, + { "QHeaderView", QPlatformTheme::HeaderPalette }, + { "QAbstractItemView", QPlatformTheme::ItemViewPalette }, + { "QMessageBoxLabel", QPlatformTheme::MessageBoxLabelPalette }, + { "QTabBar", QPlatformTheme::TabBarPalette }, + { "QLabel", QPlatformTheme::LabelPalette }, + { "QGroupBox", QPlatformTheme::GroupBoxPalette }, + { "QMenu", QPlatformTheme::MenuPalette }, + { "QMenuBar", QPlatformTheme::MenuBarPalette }, + { "QTextEdit", QPlatformTheme::TextEditPalette }, + { "QTextControl", QPlatformTheme::TextEditPalette }, + { "QLineEdit", QPlatformTheme::TextLineEditPalette }, + }; + + for (const auto themedWidget : themedWidgets) { + if (auto *palette = platformTheme->palette(themedWidget.palette)) + QApplication::setPalette(*palette, themedWidget.className); + } } /*! @@ -4413,29 +4397,8 @@ void QApplicationPrivate::translateTouchCancel(QTouchDevice *device, ulong times void QApplicationPrivate::notifyThemeChanged() { QGuiApplicationPrivate::notifyThemeChanged(); - clearSystemPalette(); - initSystemPalette(); - qt_init_tooltip_palette(); -} - -void QApplicationPrivate::sendApplicationPaletteChange(bool toAllWidgets, const char *className) -{ - if (!is_app_running || is_app_closing) - return; - QGuiApplicationPrivate::sendApplicationPaletteChange(toAllWidgets, className); - - QEvent event(QEvent::ApplicationPaletteChange); - const QWidgetList widgets = QApplication::allWidgets(); - for (auto widget : widgets) { - if (toAllWidgets || (!className && widget->isWindow()) || (className && widget->inherits(className))) - QCoreApplication::sendEvent(widget, &event); - } - -#if QT_CONFIG(graphicsview) - for (auto scene : qAsConst(scene_list)) - QCoreApplication::sendEvent(scene, &event); -#endif // QT_CONFIG(graphicsview) + qt_init_tooltip_palette(); } #if QT_CONFIG(draganddrop) diff --git a/src/widgets/kernel/qapplication_p.h b/src/widgets/kernel/qapplication_p.h index 71f695cc18..ab6d85aeb9 100644 --- a/src/widgets/kernel/qapplication_p.h +++ b/src/widgets/kernel/qapplication_p.h @@ -158,12 +158,12 @@ public: static QSize app_strut; static QWidgetList *popupWidgets; static QStyle *app_style; - static QPalette *sys_pal; protected: void notifyThemeChanged() override; - void sendApplicationPaletteChange(bool toAllWidgets = false, - const char *className = nullptr) override; + + QPalette basePalette() const override; + void handlePaletteChanged(const char *className = nullptr) override; #if QT_CONFIG(draganddrop) void notifyDragStarted(const QDrag *) override; @@ -184,9 +184,7 @@ public: static int enabledAnimations; // Combination of QPlatformTheme::UiEffect static bool widgetCount; // Coupled with -widgetcount switch - static void setSystemPalette(const QPalette &pal); - static void setPalette_helper(const QPalette &palette, const char* className); - static void initializeWidgetPaletteHash(); + static void initializeWidgetPalettesFromTheme(); static void initializeWidgetFontHash(); static void setSystemFont(const QFont &font); diff --git a/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp b/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp index 5ee25677bf..a7881b6983 100644 --- a/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp +++ b/tests/auto/gui/kernel/qguiapplication/tst_qguiapplication.cpp @@ -506,29 +506,67 @@ void tst_QGuiApplication::keyboardModifiers() window->close(); } +/* + Compare actual against expected but ignore unset roles. + + Comparing palettes via operator== will compare all roles. +*/ +static bool palettesMatch(const QPalette &actual, const QPalette &expected) +{ + if (actual.resolve() != expected.resolve()) + return false; + + for (int i = 0; i < QPalette::NColorGroups; i++) { + for (int j = 0; j < QPalette::NColorRoles; j++) { + const auto g = QPalette::ColorGroup(i); + const auto r = QPalette::ColorRole(j); + if (expected.isBrushSet(g, r)) { + if (actual.brush(g, r) != expected.brush(g, r)) + return false; + } + } + } + return true; +} + void tst_QGuiApplication::palette() { + // Getting the palette before application construction should work + QPalette paletteBeforeAppConstruction = QGuiApplication::palette(); + // And should be reflected in the default constructed palette + QCOMPARE(paletteBeforeAppConstruction, QPalette()); + int argc = 1; char *argv[] = { const_cast<char*>("tst_qguiapplication") }; QGuiApplication app(argc, argv); + + // The same should be true after application construction + QCOMPARE(QGuiApplication::palette(), QPalette()); + + // The default application palette is not resolved + QVERIFY(!QGuiApplication::palette().resolve()); + QSignalSpy signalSpy(&app, SIGNAL(paletteChanged(QPalette))); QPalette oldPalette = QGuiApplication::palette(); QPalette newPalette = QPalette(Qt::red); QGuiApplication::setPalette(newPalette); - QCOMPARE(QGuiApplication::palette(), newPalette); + QVERIFY(palettesMatch(QGuiApplication::palette(), newPalette)); QCOMPARE(signalSpy.count(), 1); - QCOMPARE(signalSpy.at(0).at(0), QVariant(newPalette)); + QVERIFY(palettesMatch(signalSpy.at(0).at(0).value<QPalette>(), newPalette)); + QCOMPARE(QGuiApplication::palette(), QPalette()); QGuiApplication::setPalette(oldPalette); - QCOMPARE(QGuiApplication::palette(), oldPalette); + QVERIFY(palettesMatch(QGuiApplication::palette(), oldPalette)); QCOMPARE(signalSpy.count(), 2); - QCOMPARE(signalSpy.at(1).at(0), QVariant(oldPalette)); + QVERIFY(palettesMatch(signalSpy.at(1).at(0).value<QPalette>(), oldPalette)); + QCOMPARE(QGuiApplication::palette(), QPalette()); QGuiApplication::setPalette(oldPalette); - QCOMPARE(QGuiApplication::palette(), oldPalette); + QVERIFY(palettesMatch(QGuiApplication::palette(), oldPalette)); QCOMPARE(signalSpy.count(), 2); + QCOMPARE(QGuiApplication::palette(), QPalette()); } void tst_QGuiApplication::font() @@ -1073,6 +1111,9 @@ void tst_QGuiApplication::testSetPaletteAttribute() QGuiApplication::setPalette(palette); QVERIFY(QCoreApplication::testAttribute(Qt::AA_SetPalette)); + + QGuiApplication::setPalette(QPalette()); + QVERIFY(!QCoreApplication::testAttribute(Qt::AA_SetPalette)); } // Test that static functions do not crash if there is no application instance. |