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

Need help on importing QML



  • Hi,
    I need some help on QML importing. I have 2 projects, name Project1 and Project2

    Project 1
    |-QmlFile1.qml
    |-qml1.qrc

    Project 2
    |-QmlFile2.qml
    |-qml2.qrc

    Is it possible to import QmlFile1.qml from Project 1 to QmlFile2.qml of Project 2?



  • Hi All,
    got a solution for the issue in another forum. Here is the link: https://stackoverflow.com/questions/63054073/need-help-on-importing-qml
    Working for me.
    Thanks anyway for your support.
    Thanks and Regards,
    Bikash


  • Moderators

    Yes. But what do you mean by "import". Are these 2 project completely separate repositories or part of a single code base?



  • Thanks @sierdzio for replying.

    • Yes, they are completely separated. So is it possible?

    • If I will make them sub-projects of a common project, is it possible?

    • (Assuming they are not part of same common project) If one of them is an active project and the other one is just a directory containing few QML files, will we be able to import the QML files from the later one and use as components in the active project?


  • Moderators

    @BikashRDas said in Need help on importing QML:

    Thanks @sierdzio for replying.

    • Yes, they are completely separated. So is it possible?

    Yes, use QML modules (qmldir) feature: https://doc.qt.io/qt-5/qtqml-modules-qmldir.html

    • If I will make them sub-projects of a common project, is it possible?

    Yes, in this case you can get it even easier: just include the QRC file from other project in your main one.

    • (Assuming they are not part of same common project) If one of them is an active project and the other one is just a directory containing few QML files, will we be able to import the QML files from the later one and use as components in the active project?

    Yes, either by importing the files (git submodule), or the qmldir method. Or you can put those other QML files on some web server and use in your main project using URLs (it's not a terribly good solution, though, for various reasons).


  • Moderators

    Just an FYI

    Make sure your QML files are uniquely named, otherwise the component, from the first referenced/loaded QML file, will always be loaded, no matter what!

    That is also true when you create a separate QQmlEngine ins a QPlugin, very annoying tbh.


  • Moderators

    @J-Hilk said in Need help on importing QML:

    Just an FYI

    Make sure your QML files are uniquely named, otherwise the component, from the first referenced/loaded QML file, will always be loaded, no matter what!

    That is also true when you create a separate QQmlEngine ins a QPlugin, very annoying tbh.

    You can use import X as Y syntax to get around this issue. Example:

    import QtQuick.Controls 2
    import "buttons" as Buttons
    
    Buttons.Button {
      // This is your custom button
    }
    
    Button {
      // This is QuickControls 2 button.
    }
    


  • I have a QML file in : QT_WS/COMMON/MyButton.qml
    My project in QT_WS/DummyQuick/DummyQuick.pro
    QT_WS/DummyQuick/main.qml

    import QtQuick 2.5
    import QtQuick.Window 2.2
    import QtQuick.Controls 1.4
    import QtQuick.Layouts 1.1
    import "../COMMON"
    
    Window {
        visible: true
        width: 800
        height: 480
        title: qsTr("Tutorial 1")
    
        MyButton {
            id: b1
        }
    }
    

    QT_WS/COMMON/MyButton.qml

    import QtQuick 2.0
    
    Item {
        width: 400
        height: 80
        Rectangle {
            id: button
            anchors.fill: parent
            color: "red"
        }
    }
    

    QT_WS/COMMON/qmldir

    MyButton MyButton.qml
    

    I am getting error:

    qrc:/main.qml:5 "../COMMON": no such directory
    

    Anything I am missing here?


  • Moderators

    @BikashRDas said in Need help on importing QML:

    Anything I am missing here?

    Apparently, no file from ../COMMON was added to the QRC. You can add them via alias to get rid of that "../".



  • Hi @sierdzio ,
    Sorry I am very new to QML. Please let me know if my understanding is correct or not.

    @sierdzio said in Need help on importing QML:

    Apparently, no file from ../COMMON was added to the QRC. You can add them via alias to get rid of that "../".

    This means, I need to add the "MyButton.qml" from QT_WS/COMMON folder to my project QRC, right? I did the same:

    <RCC>
        <qresource prefix="/">
            <file>main.qml</file>
            <file>../COMMON/MyButton.qml</file>
        </qresource>
    </RCC>
    

    above is the updated qrc file. But still issue is same. Please let me know if I am doing something wrong. Thing is when I am keeping the files within the project folder, the import is working fine just by importing the folder. containing the qml files. But in the above case it's not working.


  • Moderators

    Ah, it's not supported:

    Note that the listed resource files must be located in the same directory as the .qrc file, or one of its subdirectories.

    This is from RCC documentation. So you can't use this approach. What you can do, is create a separate QRC in your other project, and link it to your current one (in qmake, by using RESOURCES, or by copying it and using INIT_RESOURCE in code).



  • Hi All,
    got a solution for the issue in another forum. Here is the link: https://stackoverflow.com/questions/63054073/need-help-on-importing-qml
    Working for me.
    Thanks anyway for your support.
    Thanks and Regards,
    Bikash


Log in to reply