[solved] QML2 files in resource file
-
I've created a simple Qt Quick application, added a resource file:
@
<RCC>
<qresource prefix="/">
<file>qml/QmlQrc01/main.qml</file>
</qresource>
</RCC>
@and changed main.cpp:
@
#include <QtGui/QGuiApplication>
#include "qtquick2applicationviewer.h"int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);QtQuick2ApplicationViewer viewer;
//viewer.setMainQmlFile(QStringLiteral("qml/QmlQrc01/main.qml"));
viewer.setMainQmlFile(QStringLiteral("qrc:/qml/QmlQrc01/main.qml"));
viewer.showExpanded();return app.exec();
}
@If I run the application I get this message:
@
file:///E:/QtProjects/build-QmlQrc01-Desktop_Qt_5_1_0_MinGW_32bit-Debug/qrc:/qml/QmlQrc01/main.qml: File not found
@How can I run a Qt Quick application with its qml files in resource file?
-
@
viewer.setMainQmlFile(QUrl::fromUserInput("qrc://qml/QmlQrc01/main.qml"));
@ -
This fails with
@
..\QmlQrc01\main.cpp: In function 'int qMain(int, char**)':
..\QmlQrc01\main.cpp:10:73: error: no matching function for call to 'QtQuick2ApplicationViewer::setMainQmlFile(QUrl)'
viewer.setMainQmlFile(QUrl::fromUserInput("qrc:/QmlQrc01/main.qml"));
^
..\QmlQrc01\main.cpp:10:73: note: candidate is:
In file included from ..\QmlQrc01\main.cpp:2:0:
..\QmlQrc01\qtquick2applicationviewer/qtquick2applicationviewer.h:24:10: note: void QtQuick2ApplicationViewer::setMainQmlFile(const QString&)
void setMainQmlFile(const QString &file);
^
..\QmlQrc01\qtquick2applicationviewer/qtquick2applicationviewer.h:24:10: note: no known conversion for argument 1 from 'QUrl' to 'const QString&'
@I solved it by editing qtquick2applicationviewer.cpp:
@
void QtQuick2ApplicationViewer::setMainQmlFile(const QString &file)
{
d->mainQmlFile = QtQuick2ApplicationViewerPrivate::adjustPath(file);
#ifdef Q_OS_ANDROID
setSource(QUrl(QLatin1String("assets:/")+d->mainQmlFile));
#else
//setSource(QUrl::fromLocalFile(d->mainQmlFile));
setSource(QUrl::fromUserInput(d->mainQmlFile));
#endif
}
@@
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);QtQuick2ApplicationViewer viewer;
//viewer.setMainQmlFile(QStringLiteral("qml/QmlQrc01/main.qml"));
viewer.setMainQmlFile(QStringLiteral("qrc:/qml/QmlQrc01/main.qml"));
viewer.showExpanded();return app.exec();
}@
-
I have had the same problem and till I checked the qtquick2applicationviewer.cpp exactly ::setMainQmlFile().
I spend a lot of hours to find the reason because I followed the comment
at beginning of qtquick2applicationviewer.cpp "...you should NOT modify the file". :-(I don't understand the code of ::setMainQml()... and I loose my faith a little for Qt developers. Why they kill the platform-independency with the "assets:/" ?
This line exactly kills the resource system for platform dependency!
I develop a simple application for desktop and android and use for both the resource system. Loading the main.qml about "qrc:/qml/main.qml". For desktop works, for android NO because the code snipped above you see there is "assets:/" added to beginning and the final string is "assets:/qrc:/qml/main.qml". That's sh... and doesn't work.
Thanks for all functions of qtquick2applicationviewer class but setMainQmlFile() is wrong. It should be more flexible like checking at first if the passed parameter contains "qrc:" and so on.
Because I use for both, desktop and android, the resource system I changed the setMainQmlFile() to this:
@void QtQuick2ApplicationViewer::setMainQmlFile(const QString &file)
{
d->mainQmlFile = QtQuick2ApplicationViewerPrivate::adjustPath(file);
#if defined(Q_OS_ANDROID) && !defined(Q_OS_ANDROID_NO_SDK)
// setSource(QUrl(QLatin1String("assets:/")+d->mainQmlFile));
setSource(QUrl(d->mainQmlFile));
#else
// setSource(QUrl::fromLocalFile(d->mainQmlFile));
setSource(QUrl(d->mainQmlFile));
#endif
}
@ -
Personally, I prefer using just the plain QQuickView. Easy and more controlable :)