summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Faure <david.faure@kdab.com>2016-06-18 15:21:24 +0200
committerDavid Faure <david.faure@kdab.com>2016-07-06 17:20:28 +0000
commit0ea4a2cd207540c6936c6cf16bc5b371d6bcd09d (patch)
treece738f1f7cd0696229d0454b64891ed27b19b0d0
parent535f520685c2af471fd5d8d83da95f4321f171bb (diff)
downloadqtquickcontrols-0ea4a2cd207540c6936c6cf16bc5b371d6bcd09d.tar.gz
Find custom styles stored in qrc.
The unittest was showing that it was looking in <CURRENTDIR>/qrc:/qt-project.org/imports/QtQuick/Controls/Styles which makes no sense. In addition, the code was only listing the directory where the default style is found. When the custom style is elsewhere, it needs to be looked up in addition. Change-Id: I93eddab73672c575cd92037e1d0b366cf17540ef Reviewed-by: Simon Hausmann <simon.hausmann@qt.io> Reviewed-by: J-P Nurmi <jpnurmi@qt.io>
-rw-r--r--src/controls/Private/qquickcontrolsettings.cpp38
-rw-r--r--src/controls/Private/qquickcontrolsettings_p.h1
-rw-r--r--tests/auto/customcontrolsstyle/style.qrc3
-rw-r--r--tests/auto/customcontrolsstyle/tst_customcontrolsstyle.cpp1
4 files changed, 30 insertions, 13 deletions
diff --git a/src/controls/Private/qquickcontrolsettings.cpp b/src/controls/Private/qquickcontrolsettings.cpp
index dfab577e..1965738a 100644
--- a/src/controls/Private/qquickcontrolsettings.cpp
+++ b/src/controls/Private/qquickcontrolsettings.cpp
@@ -42,6 +42,7 @@
#include <qcoreapplication.h>
#include <qdebug.h>
#include <qqmlengine.h>
+#include <qfileinfo.h>
#include <qlibrary.h>
#include <qdir.h>
#include <QTouchDevice>
@@ -154,16 +155,21 @@ static QString relativeStyleImportPath(QQmlEngine *engine, const QString &styleN
QString path;
#ifndef QT_STATIC
bool found = false;
- const auto importPathList = engine->importPathList();
- for (const QString &import : importPathList) {
- QDir dir(import + QStringLiteral("/QtQuick/Controls/Styles"));
- if (dir.exists(styleName)) {
- found = true;
- path = dir.absolutePath();
- break;
+ const auto importPathList = engine->importPathList(); // ideally we'd call QQmlImportDatabase::importPathList(Local) here, but it's not exported
+ for (QString import : importPathList) {
+ bool localPath = QFileInfo(import).isAbsolute();
+ if (import.startsWith(QLatin1String("qrc:/"), Qt::CaseInsensitive)) {
+ import = QLatin1Char(':') + import.mid(4);
+ localPath = true;
+ }
+ if (localPath) {
+ QDir dir(import + QStringLiteral("/QtQuick/Controls/Styles"));
+ if (dir.exists(styleName)) {
+ found = true;
+ path = dir.absolutePath();
+ break;
+ }
}
- if (found)
- break;
}
if (!found)
path = ":/QtQuick/Controls/Styles";
@@ -194,6 +200,7 @@ static QString styleImportPath(QQmlEngine *engine, const QString &styleName)
}
QQuickControlSettings1::QQuickControlSettings1(QQmlEngine *engine)
+ : m_engine(engine)
{
// First, register all style paths in the default style location.
QDir dir;
@@ -223,10 +230,14 @@ QQuickControlSettings1::QQuickControlSettings1(QQmlEngine *engine)
if (m_styleMap.contains(m_name)) {
m_path = m_styleMap.value(m_name).m_styleDirPath;
} else {
- QString unknownStyle = m_name;
- m_name = defaultStyle;
m_path = m_styleMap.value(defaultStyle).m_styleDirPath;
- qWarning() << "WARNING: Cannot find style" << unknownStyle << "- fallback:" << styleFilePath();
+ // Maybe the requested style is not next to the default style, but elsewhere in the import path
+ findStyle(engine, m_name);
+ if (!m_styleMap.contains(m_name)) {
+ QString unknownStyle = m_name;
+ m_name = defaultStyle;
+ qWarning() << "WARNING: Cannot find style" << unknownStyle << "- fallback:" << styleFilePath();
+ }
}
// Can't really do anything about this failing here, so don't bother checking...
@@ -278,7 +289,8 @@ void QQuickControlSettings1::findStyle(QQmlEngine *engine, const QString &styleN
QDir dir;
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
dir.setPath(path);
- dir.cd(styleName);
+ if (!dir.cd(styleName))
+ return;
StyleData styleData;
diff --git a/src/controls/Private/qquickcontrolsettings_p.h b/src/controls/Private/qquickcontrolsettings_p.h
index aa3e4b2d..bfa44f2f 100644
--- a/src/controls/Private/qquickcontrolsettings_p.h
+++ b/src/controls/Private/qquickcontrolsettings_p.h
@@ -106,6 +106,7 @@ private:
QString m_name;
QString m_path;
QHash<QString, StyleData> m_styleMap;
+ QQmlEngine *m_engine;
};
QT_END_NAMESPACE
diff --git a/tests/auto/customcontrolsstyle/style.qrc b/tests/auto/customcontrolsstyle/style.qrc
index 37cfbbd7..c04782ba 100644
--- a/tests/auto/customcontrolsstyle/style.qrc
+++ b/tests/auto/customcontrolsstyle/style.qrc
@@ -2,4 +2,7 @@
<qresource prefix="/">
<file>Style/ButtonStyle.qml</file>
</qresource>
+ <qresource prefix="/qt-project.org/imports/QtQuick/Controls/Styles/ResourceStyle">
+ <file alias="ButtonStyle.qml">Style/ButtonStyle.qml</file>
+ </qresource>
</RCC>
diff --git a/tests/auto/customcontrolsstyle/tst_customcontrolsstyle.cpp b/tests/auto/customcontrolsstyle/tst_customcontrolsstyle.cpp
index ebd60d1e..99e8240e 100644
--- a/tests/auto/customcontrolsstyle/tst_customcontrolsstyle.cpp
+++ b/tests/auto/customcontrolsstyle/tst_customcontrolsstyle.cpp
@@ -61,6 +61,7 @@ void tst_customcontrolsstyle::style_data()
QTest::newRow("NonExistentStyle") << QString::fromLatin1("NonExistentStyle") << QString::fromLatin1("Base");
QTest::newRow("CustomFileSystemStyle") << directory() + QString::fromLatin1("/Style") << QString::fromLatin1("Style");
+ QTest::newRow("BuiltinQrcStyle") << QString::fromLatin1("ResourceStyle") << QString::fromLatin1("ResourceStyle"); // from :/qt-project.org/imports/QtQuick/Controls/Styles
QTest::newRow("CustomQrcStyle") << QString::fromLatin1(":/Style") << QString::fromLatin1("Style");
}