Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [solved] QML2 files in resource file
Forum Updated to NodeBB v4.3 + New Features

[solved] QML2 files in resource file

Scheduled Pinned Locked Moved QML and Qt Quick
5 Posts 3 Posters 3.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rubikon
    wrote on last edited by
    #1

    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&#40;&#41;;
    

    }
    @

    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?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      @
      viewer.setMainQmlFile(QUrl::fromUserInput("qrc://qml/QmlQrc01/main.qml"));
      @

      (Z(:^

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rubikon
        wrote on last edited by
        #3

        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();
        

        }@

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jackmack
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            Personally, I prefer using just the plain QQuickView. Easy and more controlable :)

            (Z(:^

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved