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 <ulf.hermann@qt.io>
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
(cherry picked from commit fdd62a5b81)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Mitch Curtis 2022-05-02 15:42:53 +08:00 committed by Qt Cherry-pick Bot
parent 10aa86b7de
commit 5ebe130bbe
5 changed files with 101 additions and 0 deletions

View File

@ -34,6 +34,7 @@
**
******************************************************************************/
#include <QtCore/private/qfileselector_p.h>
#include <QtCore/qloggingcategory.h>
#include <QtQml/qqmlengine.h>
#include <QtQml/qqmlextensionplugin.h>
@ -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()

View File

@ -0,0 +1,5 @@
import QtQuick
Item {
objectName: "+FileSystemStyle/CustomComponent.qml"
}

View File

@ -0,0 +1,5 @@
import QtQuick
Item {
objectName: "CustomComponent.qml"
}

View File

@ -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
}
}

View File

@ -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<QQuickWindow*>(engine.rootObjects().first());
QVERIFY(window);
QObject *customComponent = window->property("customComponent").value<QObject*>();
QVERIFY(customComponent);
QCOMPARE(customComponent->objectName(), "+FileSystemStyle/CustomComponent.qml");
}
void tst_StyleImports::fallbackStyleShouldNotOverwriteTheme_data()
{
QTest::addColumn<QString>("style");