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

  • 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,


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

  • 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?


  • 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.

  • 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.

  • 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.

  • Thanks Nikrolls.

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

  • 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"?


Log in to reply