on xenial help sign in
Added prependPaths. shellImportPaths returns a list.
commit8cbd93454bf961bdf22c4eb795dfe431e1b4caab authorNick Dedekind <nicholas.dedekind@gmail.com> dateWed 26 Jun 2013, 14:02 (13 years ago) parent7386fbb
2 files changed+47−9
main.cpp+37−6 paths.h.in+10−3
main.cpp+37−6
@@ -31,13 +31,33 @@
3131#include "MouseTouchAdaptor.h"
3232
3333namespace {
34+
35+void prependImportPaths(QQmlEngine *engine, const QStringList &paths)
36+{
37+ QStringList importPathList = engine->importPathList();
38+ for (int i = paths.count() -1; i >= 0; i--) {
39+ // don't duplicate
40+ const QString& path = paths[i];
41+ QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
42+ if (iter == importPathList.end())
43+ importPathList.prepend(path);
44+ }
45+ engine->setImportPathList(importPathList);
46+}
47+
3448/* When you append and import path to the list of import paths it will be the *last*
3549 place where Qt will search for QML modules.
3650 The usual QQmlEngine::addImportPath() actually prepends the given path.*/
37−void appendImportPath(QQmlEngine *engine, const QString &path)
51+void appendImportPaths(QQmlEngine *engine, const QStringList &paths)
3852{
3953 QStringList importPathList = engine->importPathList();
40− importPathList.append(path);
54+ Q_FOREACH(const QString& path, paths) {
55+ // don't duplicate
56+ QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
57+ if (iter == importPathList.end())
58+ importPathList.append(path);
59+ }
60+ importPathList.append(paths);
4161 engine->setImportPathList(importPathList);
4262}
4363
@@ -88,7 +108,7 @@ int main(int argc, char** argv)
88108 QQuickView* view = new QQuickView();
89109 view->setResizeMode(QQuickView::SizeRootObjectToView);
90110 view->setTitle("Qml Phone Shell");
91− view->engine()->setBaseUrl(QUrl::fromLocalFile(shellAppDirectory()));
111+ view->engine()->setBaseUrl(QUrl::fromLocalFile(::shellAppDirectory()));
92112 if (args.contains(QLatin1String("-frameless"))) {
93113 view->setFlags(Qt::FramelessWindowHint);
94114 }
@@ -113,13 +133,24 @@ int main(int argc, char** argv)
113133
114134 QObject::connect(view->engine(), SIGNAL(quit()), qApp, SLOT(quit()));
115135
136+
137+ Q_FOREACH(QString import, view->engine()->importPathList())
138+ {
139+ printf("BEFORE IMPORT - %s\n", import.toLatin1().data());
140+ }
141+
116142 QUrl source("Shell.qml");
117− view->engine()->addImportPath(shellAppDirectory());
118− view->engine()->addImportPath(shellImportPath());
119− appendImportPath(view->engine(), fakePluginsImportPath());
143+ prependImportPaths(view->engine(), QStringList() << ::shellAppDirectory());
144+ prependImportPaths(view->engine(), shellImportPaths());
145+ appendImportPaths(view->engine(), QStringList() << ::fakePluginsImportPath());
120146 view->setSource(source);
121147 view->setColor("transparent");
122148
149+ Q_FOREACH(QString import, view->engine()->importPathList())
150+ {
151+ printf("AFTER IMPORT - %s\n", import.toLatin1().data());
152+ }
153+
123154 if (qgetenv("QT_QPA_PLATFORM") == "ubuntu" || args.contains(QLatin1String("-fullscreen"))) {
124155 view->showFullScreen();
125156 } else {
paths.h.in+10−3
@@ -17,6 +17,9 @@
1717 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1818 */
1919
20+#ifndef PATHS_H
21+#define PATHS_H
22+
2023// Qt
2124#include <QtCore/QCoreApplication>
2225#include <QtCore/QDir>
@@ -43,12 +46,14 @@ inline QString shellAppDirectory() {
4346 }
4447}
4548
46−inline QString shellImportPath() {
49+inline QStringList shellImportPaths() {
50+ QStringList paths;
4751 if (isRunningInstalled()) {
48− return QString("@CMAKE_INSTALL_PREFIX@/@SHELL_APP_DIR@/plugins");
52+ paths << QString("@CMAKE_INSTALL_PREFIX@/@SHELL_APP_DIR@/plugins");
4953 } else {
50− return QString("@CMAKE_BINARY_DIR@/plugins/");
54+ paths << QString("@CMAKE_BINARY_DIR@/plugins");
5155 }
56+ return paths;
5257}
5358
5459inline QString fakePluginsImportPath() {
@@ -62,3 +67,5 @@ inline QString fakePluginsImportPath() {
6267inline QString sourceDirectory() {
6368 return QString("@CMAKE_SOURCE_DIR@/");
6469}
70+
71+#endif