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. When main.qml is inside a resource file, how to import local files which are not inside the resource ?
Forum Updated to NodeBB v4.3 + New Features

When main.qml is inside a resource file, how to import local files which are not inside the resource ?

Scheduled Pinned Locked Moved QML and Qt Quick
7 Posts 4 Posters 13.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.
  • Z Offline
    Z Offline
    zanes
    wrote on last edited by
    #1

    Hi All,

    I have placed my main.qml file inside a resource file and I’m using the below code in main to run my application.

    @ viewer->setSource(QUrl("qrc:/qml/qtpathtest/main.qml"));@

    Now, in main.qml, I need to import some qml component directories, which are not inside the resource file, so i added the import line
    @
    import "../comp"@

    It works fine, when i view the main.qml in a qml viewer but not when i run the application from qtcreator !
    Any ideas,

    Thanks

    [edit : added @ tags around code, please add them next time, Eddy]

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mbrasser
      wrote on last edited by
      #2

      If main.qml is inside a resource file, the engine will assume that all relative paths in QML should also be resolved within that resource file (see http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html#managing-resource-files-with-the-qt-resource-system for more information on QML and the Qt Resource System).

      Can you give any more details about your structure -- e.g. why you need to have some files inside the resource system and some outside?

      Regards,
      Michael

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        If you need to access files from the outside of your resource, you will need to make sure that your QML "knows" where to locate these. You could do that by letting the C++ side of your application set a string value with the application path or whatever the base path is of your non-resource files that you need to access.

        1 Reply Last reply
        0
        • Z Offline
          Z Offline
          zanes
          wrote on last edited by
          #4

          Thanks mbrasser,
          I like to have my qml files inside the resource so that i can protect the source code and for deployment. I also have some themes and stylesheets made of qml and i like those outside the resource.

          Thanks Andre, I'll try that.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nikrolls
            wrote on last edited by
            #5

            I do this for my apps because I like to protect my QML files, but don't want images included in the binary (longer load times) and don't mind people being able to see them. I use this in the main.cpp file right before loading the main.qml file:
            @viewer->rootContext()->setContextProperty("installPath", "file://" + QCoreApplication::applicationDirPath() + "/");@
            Then when referencing an image:
            @source: installPath + "../images/image.png"@

            I've never used this with import, however -- I put all my .js, .css and .qml files in the resource.

            1 Reply Last reply
            0
            • Z Offline
              Z Offline
              zanes
              wrote on last edited by
              #6

              Thanks Nikrolls.

              Now I also found that i can use js to get the files which are not inside the resource file in qml.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                mbrasser
                wrote on last edited by
                #7

                Andre, nikrools,

                Could the QML environment be improved to support this use case better (and if so, any suggestions?), or do you feel like the current solution you are using is "good enough"?

                Thanks,
                Michael

                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