From 5ebe130bbe1b2edd23933ef64e2cddd9fa420030 Mon Sep 17 00:00:00 2001 From: Mitch Curtis Date: Mon, 2 May 2022 15:42:53 +0800 Subject: [PATCH] Add file selector for custom styles when using run-time style selection Previously this was only available for QQuickStylePlugin-based styles. This patch makes it available for qmldir-based styles when using run-time style selection. The existing documentation doesn't differentiate between built-in styles and custom styles: "Qt Quick Controls extends the built-in selectors with the name (lowercase) of the style that an application is running with." So this is a bug fix. Change-Id: Ib2ed56ad8398db49d192db6a6023d776caaa4457 Fixes: QTBUG-99117 Reviewed-by: Ulf Hermann Reviewed-by: Richard Moe Gustavsen (cherry picked from commit fdd62a5b81d5c3de423118bb24d5ac18a7fddbe5) Reviewed-by: Qt Cherry-pick Bot --- src/quickcontrols2/qtquickcontrols2plugin.cpp | 4 ++ .../data/+FileSystemStyle/CustomComponent.qml | 5 ++ .../styleimports/data/CustomComponent.qml | 5 ++ .../styleimports/data/customStyleSelector.qml | 64 +++++++++++++++++++ .../styleimports/tst_styleimports.cpp | 23 +++++++ 5 files changed, 101 insertions(+) create mode 100644 tests/auto/quickcontrols2/styleimports/data/+FileSystemStyle/CustomComponent.qml create mode 100644 tests/auto/quickcontrols2/styleimports/data/CustomComponent.qml create mode 100644 tests/auto/quickcontrols2/styleimports/data/customStyleSelector.qml diff --git a/src/quickcontrols2/qtquickcontrols2plugin.cpp b/src/quickcontrols2/qtquickcontrols2plugin.cpp index b5bceb6bb5..1fd76a5316 100644 --- a/src/quickcontrols2/qtquickcontrols2plugin.cpp +++ b/src/quickcontrols2/qtquickcontrols2plugin.cpp @@ -34,6 +34,7 @@ ** ******************************************************************************/ +#include #include #include #include @@ -145,6 +146,9 @@ void QtQuickControls2Plugin::registerTypes(const char *uri) << " uri \"" << qtQuickControlsUri << "\" moduleMajor " << importMajor << " import " << registeredStyleUri << " importMajor " << importMajor; qmlRegisterModuleImport(qtQuickControlsUri, QQmlModuleImportModuleAny, registeredStyleUri.toUtf8().constData(), importMajor); + + if (customStyle) + QFileSelectorPrivate::addStatics(QStringList() << styleName); } void QtQuickControls2Plugin::unregisterTypes() diff --git a/tests/auto/quickcontrols2/styleimports/data/+FileSystemStyle/CustomComponent.qml b/tests/auto/quickcontrols2/styleimports/data/+FileSystemStyle/CustomComponent.qml new file mode 100644 index 0000000000..ee5ffe1151 --- /dev/null +++ b/tests/auto/quickcontrols2/styleimports/data/+FileSystemStyle/CustomComponent.qml @@ -0,0 +1,5 @@ +import QtQuick + +Item { + objectName: "+FileSystemStyle/CustomComponent.qml" +} diff --git a/tests/auto/quickcontrols2/styleimports/data/CustomComponent.qml b/tests/auto/quickcontrols2/styleimports/data/CustomComponent.qml new file mode 100644 index 0000000000..220630f812 --- /dev/null +++ b/tests/auto/quickcontrols2/styleimports/data/CustomComponent.qml @@ -0,0 +1,5 @@ +import QtQuick + +Item { + objectName: "CustomComponent.qml" +} diff --git a/tests/auto/quickcontrols2/styleimports/data/customStyleSelector.qml b/tests/auto/quickcontrols2/styleimports/data/customStyleSelector.qml new file mode 100644 index 0000000000..7d36842131 --- /dev/null +++ b/tests/auto/quickcontrols2/styleimports/data/customStyleSelector.qml @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2022 The Qt Company Ltd. +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:BSD$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** BSD License Usage +** Alternatively, you may use this file under the terms of the BSD license +** as follows: +** +** "Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions are +** met: +** * Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** * Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in +** the documentation and/or other materials provided with the +** distribution. +** * Neither the name of The Qt Company Ltd nor the names of its +** contributors may be used to endorse or promote products derived +** from this software without specific prior written permission. +** +** +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick +import QtQuick.Controls + +ApplicationWindow { + title: "Test Application Window" + width: 400 + height: 400 + + property alias customComponent: customComponent + + CustomComponent { + id: customComponent + } +} diff --git a/tests/auto/quickcontrols2/styleimports/tst_styleimports.cpp b/tests/auto/quickcontrols2/styleimports/tst_styleimports.cpp index b09d4f86a1..bdd3a40cea 100644 --- a/tests/auto/quickcontrols2/styleimports/tst_styleimports.cpp +++ b/tests/auto/quickcontrols2/styleimports/tst_styleimports.cpp @@ -62,6 +62,7 @@ private slots: void select(); void platformSelectors(); + void customStyleSelector(); void fallbackStyleShouldNotOverwriteTheme_data(); void fallbackStyleShouldNotOverwriteTheme(); @@ -180,6 +181,7 @@ void tst_StyleImports::select() } } +// Tests that the various platforms are available as selectors. void tst_StyleImports::platformSelectors() { QQuickStyle::setStyle(QLatin1String("PlatformStyle")); @@ -205,6 +207,27 @@ void tst_StyleImports::platformSelectors() #endif } +// Tests that a file selector is added for custom styles. +// Note that this is different to the regular QML import mechanism +// that results in e.g. FileSystemStyle/Button.qml being found; +// it allows non-template (Controls), custom user types to be +// picked up via selectors. +void tst_StyleImports::customStyleSelector() +{ + QQuickStyle::setStyle(QLatin1String("FileSystemStyle")); + + QQmlApplicationEngine engine; + engine.addImportPath(dataDirectory() + QLatin1String("/styles")); + engine.load(testFileUrl("customStyleSelector.qml")); + QVERIFY(!engine.rootObjects().isEmpty()); + QQuickWindow *window = qobject_cast(engine.rootObjects().first()); + QVERIFY(window); + + QObject *customComponent = window->property("customComponent").value(); + QVERIFY(customComponent); + QCOMPARE(customComponent->objectName(), "+FileSystemStyle/CustomComponent.qml"); +} + void tst_StyleImports::fallbackStyleShouldNotOverwriteTheme_data() { QTest::addColumn("style");