aboutsummaryrefslogtreecommitdiffstats
path: root/src/quickcontrols2
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@qt.io>2020-09-11 16:18:50 +0200
committerMitch Curtis <mitch.curtis@qt.io>2020-09-24 13:48:58 +0200
commita0f0b4f65e96aecc9949d3089a283cdfaeefbb3b (patch)
tree175f986e31860164c000b9a66c09a549b4f5da55 /src/quickcontrols2
parent526729cb8a53972b6925cbfd7a5f5cbf124be71c (diff)
Support compile-time style selection
By allowing importing styles without first importing QtQuick.Controls, which does runtime style selection. [ChangeLog][Styles] It's now possible to select a style at compile-time by importing that style explicitly instead of QtQuick.Controls. This avoids the need to do run-time style selection and hence deploy the QtQuick.Controls plugin with the application. Change-Id: I666d6dc7727fffd2c7b05743855f2086f076465a Fixes: QTBUG-86284 Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src/quickcontrols2')
-rw-r--r--src/quickcontrols2/qquickstyle.cpp3
-rw-r--r--src/quickcontrols2/qquickstyleplugin.cpp75
-rw-r--r--src/quickcontrols2/qquickstyleplugin_p.h2
3 files changed, 75 insertions, 5 deletions
diff --git a/src/quickcontrols2/qquickstyle.cpp b/src/quickcontrols2/qquickstyle.cpp
index d5ded6c2..ca844c6f 100644
--- a/src/quickcontrols2/qquickstyle.cpp
+++ b/src/quickcontrols2/qquickstyle.cpp
@@ -91,6 +91,9 @@ Q_LOGGING_CATEGORY(lcQtQuickControlsStyle, "qt.quick.controls.style")
Qt Quick Controls. It is not possible to change the style after the QML
types have been registered.
+ \note QQuickStyle is not supported when using
+ \l {Compile-Time Style Selection}{compile-time style selection}.
+
To create your own custom style, see \l {Creating a Custom Style}. Custom
styles do not need to implement all controls. By default, the styling
system uses the \l {Basic style} as a fallback for controls that a custom
diff --git a/src/quickcontrols2/qquickstyleplugin.cpp b/src/quickcontrols2/qquickstyleplugin.cpp
index f211d4b0..730a8423 100644
--- a/src/quickcontrols2/qquickstyleplugin.cpp
+++ b/src/quickcontrols2/qquickstyleplugin.cpp
@@ -38,9 +38,11 @@
#include "qquickstyle_p.h"
#include "qquickstyleplugin_p.h"
+#include <QtCore/private/qfileselector_p.h>
#include <QtCore/qloggingcategory.h>
#include <QtQml/qqmlengine.h>
#include <QtQml/qqmlfile.h>
+#include <QtQml/private/qqmlmetatype_p.h>
#include <QtQuickTemplates2/private/qquicktheme_p_p.h>
QT_BEGIN_NAMESPACE
@@ -60,25 +62,88 @@ void QQuickStylePlugin::registerTypes(const char *uri)
{
qCDebug(lcStylePlugin).nospace() << "registerTypes called with uri " << uri << "; plugin name is " << name();
+ const QTypeRevision latestControlsRevision = QQmlMetaType::latestModuleVersion(QLatin1String("QtQuick.Controls"));
+ QString styleName = QQuickStyle::name();
+ if (!latestControlsRevision.isValid() && styleName.isEmpty()) {
+ // The user hasn't imported QtQuick.Controls, nor set a style via the runtime methods.
+ qCDebug(lcStylePlugin).nospace() << uri << " imported before QtQuick.Controls; using compile-time style selection";
+ QQuickStyle::setStyle(name());
+ styleName = name();
+ }
+
+ // Even if this style plugin isn't for the style set by the user,
+ // we still want to create the theme object, because that function
+ // is also responsible for reading values from qtquickcontrols2.conf.
+ // So, even if a style doesn't have a QQuickTheme, it can still have
+ // values set for (e.g. fonts and palettes) in qtquickcontrols2.conf.
+ const QString effectiveCurrentStyleName = QQuickStylePrivate::effectiveStyleName(styleName);
auto theme = QQuickTheme::instance();
if (!theme) {
- qWarning() << "QtQuick.Controls must be imported before importing" << baseUrl().toString();
- return;
+ qCDebug(lcStylePlugin) << "creating theme";
+ theme = createTheme(effectiveCurrentStyleName);
}
- if (name() != QQuickStyle::name()) {
+ if (name() != effectiveCurrentStyleName) {
qCDebug(lcStylePlugin).nospace() << "theme does not belong to current style ("
- << QQuickStyle::name() << "); not calling initializeTheme()";
+ << effectiveCurrentStyleName << "); not calling initializeTheme()";
return;
}
- qCDebug(lcStylePlugin) << "theme has not been initialized; calling initializeTheme()";
+ qCDebug(lcStylePlugin) << "theme has not yet been initialized; calling initializeTheme()";
initializeTheme(theme);
+
+ if (!styleName.isEmpty())
+ QFileSelectorPrivate::addStatics(QStringList() << styleName);
}
void QQuickStylePlugin::unregisterTypes()
{
qCDebug(lcStylePlugin) << "unregisterTypes called; plugin name is" << name();
+ if (!QQuickThemePrivate::instance)
+ return;
+
+ // Not every style has a plugin - some styles are QML-only. So, we clean this
+ // stuff up when the first style plugin is unregistered rather than when the
+ // plugin for the current style is unregistered.
+ QQuickThemePrivate::instance.reset();
+ QQuickStylePrivate::reset();
+}
+
+/*!
+ \internal
+
+ Responsible for setting the font and palette settings that were specified in the
+ qtquickcontrols2.conf file.
+
+ Style-specific settings (e.g. Variant=Dense) are read in the constructor of the
+ appropriate style plugin (e.g. QtQuickControls2MaterialStylePlugin).
+
+ Implicit style-specific font and palette values are assigned in the relevant theme
+ (e.g. QQuickMaterialTheme).
+*/
+QQuickTheme *QQuickStylePlugin::createTheme(const QString &name)
+{
+ qCDebug(lcStylePlugin) << "creating QQuickTheme instance to be initialized by style-specific theme of" << name;
+
+ QQuickTheme *theme = new QQuickTheme;
+#if QT_CONFIG(settings)
+ QQuickThemePrivate *p = QQuickThemePrivate::get(theme);
+ QSharedPointer<QSettings> settings = QQuickStylePrivate::settings(name);
+ if (settings) {
+ p->defaultFont.reset(QQuickStylePrivate::readFont(settings));
+ // Set the default font as the System scope, because that's what
+ // QQuickControlPrivate::parentFont() uses as its fallback if no
+ // parent item has a font explicitly set. QQuickControlPrivate::parentFont()
+ // is used as the starting point for font inheritance/resolution.
+ // The same goes for palettes below.
+ theme->setFont(QQuickTheme::System, *p->defaultFont);
+
+ p->defaultPalette.reset(QQuickStylePrivate::readPalette(settings));
+ theme->setPalette(QQuickTheme::System, *p->defaultPalette);
+ }
+#endif
+ QQuickThemePrivate::instance.reset(theme);
+ return theme;
}
QT_END_NAMESPACE
diff --git a/src/quickcontrols2/qquickstyleplugin_p.h b/src/quickcontrols2/qquickstyleplugin_p.h
index 1a941aa7..0cc793a2 100644
--- a/src/quickcontrols2/qquickstyleplugin_p.h
+++ b/src/quickcontrols2/qquickstyleplugin_p.h
@@ -70,6 +70,8 @@ public:
void unregisterTypes() override;
private:
+ QQuickTheme *createTheme(const QString &name);
+
Q_DISABLE_COPY(QQuickStylePlugin)
};