diff options
author | Mitch Curtis <mitch.curtis@qt.io> | 2024-05-06 15:38:53 +0800 |
---|---|---|
committer | Mitch Curtis <mitch.curtis@qt.io> | 2024-05-08 09:04:50 +0800 |
commit | 0ad15c4101889a22bf9ff761e7d20ec515f46514 (patch) | |
tree | 45f543c83abed10b99216eefb9ca3286f1f6ec1a | |
parent | 8d0b55e988a38d0f1125d677b356fefaf743d2a3 (diff) |
ComboBox: fix popup palette inheritance
54a2a35f17d5e5d1793af63b76993a583e10ab41 fixed the theme/accent/primary
inheritance of the Material and Universal style's ComboBox popups. This
patch makes an equivalent fix for styles that use palettes. Now the
Basic and Fusion ComboBox popups will have the correct colors in both
light and dark modes.
Fixes: QTBUG-124474
Pick-to: 6.5 6.7
Change-Id: I3e3fcdc9d6fe75f195cc4f4707eac6d30413121f
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
5 files changed, 133 insertions, 5 deletions
diff --git a/src/quickcontrols/basic/ComboBox.qml b/src/quickcontrols/basic/ComboBox.qml index 5c71a4398e..7c272a80e8 100644 --- a/src/quickcontrols/basic/ComboBox.qml +++ b/src/quickcontrols/basic/ComboBox.qml @@ -85,6 +85,7 @@ T.ComboBox { height: Math.min(contentItem.implicitHeight, control.Window.height - topMargin - bottomMargin) topMargin: 6 bottomMargin: 6 + palette: control.palette contentItem: ListView { clip: true @@ -98,14 +99,14 @@ T.ComboBox { width: parent.width height: parent.height color: "transparent" - border.color: control.palette.mid + border.color: palette.mid } T.ScrollIndicator.vertical: ScrollIndicator { } } background: Rectangle { - color: control.palette.window + color: palette.window } } } diff --git a/src/quickcontrols/fusion/ComboBox.qml b/src/quickcontrols/fusion/ComboBox.qml index 609f294d6f..55d91e65ed 100644 --- a/src/quickcontrols/fusion/ComboBox.qml +++ b/src/quickcontrols/fusion/ComboBox.qml @@ -116,6 +116,7 @@ T.ComboBox { topMargin: 6 bottomMargin: 6 padding: 1 + palette: control.palette contentItem: ListView { clip: true @@ -129,15 +130,15 @@ T.ComboBox { } background: Rectangle { - color: control.popup.palette.window - border.color: Fusion.outline(control.palette) + color: palette.window + border.color: Fusion.outline(palette) Rectangle { z: -1 x: 1; y: 1 width: parent.width height: parent.height - color: control.palette.shadow + color: palette.shadow opacity: 0.2 } } diff --git a/tests/auto/quickcontrols/palette/data/comboBoxPopupWithApplicationWindow.qml b/tests/auto/quickcontrols/palette/data/comboBoxPopupWithApplicationWindow.qml new file mode 100644 index 0000000000..436d3cdad6 --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/comboBoxPopupWithApplicationWindow.qml @@ -0,0 +1,32 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +import QtQuick +import QtQuick.Controls + +ApplicationWindow { + width: 400 + height: 400 + + property alias topLevelComboBox: topLevelComboBox + property alias popup: popup + property alias comboBoxInPopup: comboBoxInPopup + + ComboBox { + id: topLevelComboBox + model: ["ONE", "TWO", "THREE"] + } + + Popup { + id: popup + width: 200 + height: 200 + visible: true + palette.window: "red" + + ComboBox { + id: comboBoxInPopup + model: ["ONE", "TWO", "THREE"] + } + } +} diff --git a/tests/auto/quickcontrols/palette/data/comboBoxPopupWithWindow.qml b/tests/auto/quickcontrols/palette/data/comboBoxPopupWithWindow.qml new file mode 100644 index 0000000000..d806f30d01 --- /dev/null +++ b/tests/auto/quickcontrols/palette/data/comboBoxPopupWithWindow.qml @@ -0,0 +1,33 @@ +// Copyright (C) 2024 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only + +import QtQuick +import QtQuick.Window +import QtQuick.Controls + +Window { + width: 400 + height: 400 + + property alias topLevelComboBox: topLevelComboBox + property alias popup: popup + property alias comboBoxInPopup: comboBoxInPopup + + ComboBox { + id: topLevelComboBox + model: ["ONE", "TWO", "THREE"] + } + + Popup { + id: popup + width: 200 + height: 200 + visible: true + palette.window: "red" + + ComboBox { + id: comboBoxInPopup + model: ["ONE", "TWO", "THREE"] + } + } +} diff --git a/tests/auto/quickcontrols/palette/tst_palette.cpp b/tests/auto/quickcontrols/palette/tst_palette.cpp index 5109ad0f8f..75ee023bb7 100644 --- a/tests/auto/quickcontrols/palette/tst_palette.cpp +++ b/tests/auto/quickcontrols/palette/tst_palette.cpp @@ -7,9 +7,11 @@ #include <QtQml/qqmlengine.h> #include <QtQml/qqmlcomponent.h> #include <QtQuickTestUtils/private/qmlutils_p.h> +#include <QtQuickTestUtils/private/visualtestutils_p.h> #include <QtQuickControlsTestUtils/private/controlstestutils_p.h> #include <QtQuick/private/qquickitem_p.h> #include <QtQuickTemplates2/private/qquickapplicationwindow_p.h> +#include <QtQuickTemplates2/private/qquickcombobox_p.h> #include <QtQuickTemplates2/private/qquickcontrol_p.h> #include <QtQuickTemplates2/private/qquickcontrol_p_p.h> #include <QtQuickTemplates2/private/qquickpopup_p.h> @@ -19,6 +21,7 @@ #include <QtQuickControls2/qquickstyle.h> #include <QSignalSpy> +using namespace QQuickVisualTestUtils; using namespace QQuickControlsTestUtils; class tst_palette : public QQmlDataTest @@ -52,6 +55,9 @@ private slots: void resetColor(); void updateBindingPalette(); + + void comboBoxPopup_data(); + void comboBoxPopup(); }; tst_palette::tst_palette() @@ -551,6 +557,61 @@ void tst_palette::updateBindingPalette() QCOMPARE(windowPalette->buttonText(), customPalette->buttonText()); } +void tst_palette::comboBoxPopup_data() +{ + QTest::addColumn<QString>("style"); + QTest::addColumn<QString>("qmlFilePath"); + + QTest::newRow("Window, Basic") << "Basic" << "comboBoxPopupWithWindow.qml"; + QTest::newRow("ApplicationWindow, Basic") << "Basic" << "comboBoxPopupWithApplicationWindow.qml"; + QTest::newRow("Window, Fusion") << "Fusion" << "comboBoxPopupWithWindow.qml"; + QTest::newRow("ApplicationWindow, Fusion") << "Fusion" << "comboBoxPopupWithApplicationWindow.qml"; +} + +// Unlike regular popups, which should inherit their palette from the window and not the parent popup, +// combo box popups should inherit their palette from the combo box itself. +void tst_palette::comboBoxPopup() +{ + QFETCH(QString, style); + QFETCH(QString, qmlFilePath); + + qmlClearTypeRegistrations(); + QQuickStyle::setStyle(style); + + QQuickApplicationHelper helper(this, qmlFilePath); + QVERIFY2(helper.ready, helper.failureMessage()); + QQuickWindow *window = helper.window; + window->show(); + QVERIFY(QTest::qWaitForWindowExposed(window)); + + const auto *windowPalette = window->property("palette").value<QQuickPalette *>(); + QVERIFY(windowPalette); + + const auto *popup = window->property("popup").value<QQuickPopup *>(); + QVERIFY(popup); + const auto *popupBackground = popup->background(); + QCOMPARE(popupBackground->property("color"), QColorConstants::Red); + QCOMPARE(popupBackground->property("palette").value<QQuickPalette*>()->toQPalette().window().color(), + QColorConstants::Red); + + // This has the default palette. + const auto *topLevelComboBox = window->property("topLevelComboBox").value<QQuickComboBox *>(); + QVERIFY(topLevelComboBox); + const auto *topLevelComboBoxBackground = topLevelComboBox->popup()->background(); + QCOMPARE_NE(topLevelComboBoxBackground->property("color"), QColorConstants::Red); + QCOMPARE_NE(topLevelComboBoxBackground->property("palette").value<QQuickPalette*>()->toQPalette().window().color(), + QColorConstants::Red); + + // The popup that this combo box is in has its window role set to red, + // so the combo box's popup background should be red too. + const auto *comboBoxInPopup = window->property("comboBoxInPopup").value<QQuickComboBox *>(); + QVERIFY(comboBoxInPopup); + const auto *comboBoxInPopupBackground = comboBoxInPopup->popup()->background(); + QCOMPARE(comboBoxInPopupBackground->property("color"), QColorConstants::Red); + QCOMPARE(comboBoxInPopupBackground->property("palette").value<QQuickPalette*>()->toQPalette().window().color(), + QColorConstants::Red); +} + QTEST_MAIN(tst_palette) #include "tst_palette.moc" |