Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Importing qml



  • Hi,
    I am having problem with importing of a qml file in another file.

    My folder structure is as follows:
    Project
    -Build
    - SourceProject
    -main.qml
    -qml.qrc
    -ButtonFile.qml
    -Source
    -Widgets
    -PopupWidget
    -Popup.qml

    I want to import ButtonFile.qml in my Popup.qml,
    qml.qrc file is as follows:
    <RCC>
    <qresource prefix="/">
    <file>main.qml</file>
    <file>ButtonFile.qml</file>
    <file>../../Source/Widgets/PopupWidget/Popup.qml</file>
    </qresource>

    So having this as folder structure how do i import ButtonFile.qml in the file Popup.qml?


  • Moderators

    Perhaps this will work:

    import ":/"
    // or
    import "qrc://"
    // or
    import "../../.."
    
    // some code
    ButtonFile {
    
    }
    

    Please show your structure in some other way the current list is quite hard to understand.


  • Moderators

    hi @Anita

    from what you wrote, it seems like your Popup.qml inside your project folder is not the one inside your qrc file.

    If you fix your qrc file, you shouldn't have to import anything.



  • @sierdzio
    I have 2 main folder in my Project : Build and Source.
    The Build Folder Consists of a Folder SourceProject .which contains the files main.qml,qml.qrc, ButtonFile.qml.
    The Source Folder consists of a Folder named Widgets which contains another folder named PopUpWidgets, and PopupWidgets Contains Popup.qml.

    I tried all the three import statements, during runtime it gives an error "no such directory" for all the three import statements.



  • @J-Hilk

    Popup.qml is not in the same folder as in the main.qml,qml.qrc,buttonfile.qml.
    The file is placed in a different folder than that of the main.qml.


  • Moderators

    @Anita

    import "../../Build/SourceProject"



  • @J-Hilk

    I tried this , during runtime i got an error:
    "../../Build/SourceProject/": no such directory

    I have tried import "qrc:/../../../Build/SourceProject/" as well im getting the same error.

    I changed the path of the buttonFile.qml in qrc file from <file>ButtonWidget.qml</file> to <file>../../Build/SourceProject/ButtonWidget.qml</file> and imported the qml as "qrc:/../../../Build/SourceProject/" it worked.

    Is it necessary for the import path that we mention in the file and the path in qml.qrc file be the same?


  • Moderators

    @Anita said in Importing qml:

    Is it necessary for the import path that we mention in the file and the path in qml.qrc file be the same?

    Yes, unless you create an alias, like this:

    <file alias="Some_name.qml">long/path/to/file/File.qml</file>
    

    The alias can even change the file name and extension (or have no extension at all - but not sure if QML engine will understand it).


Log in to reply