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. Need help on importing QML
Forum Updated to NodeBB v4.3 + New Features

Need help on importing QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
11 Posts 3 Posters 910 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.
  • B Offline
    B Offline
    BikashRDas
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      BikashRDas
      wrote on last edited by BikashRDas
      #11

      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

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

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

        (Z(:^

        1 Reply Last reply
        0
        • B Offline
          B Offline
          BikashRDas
          wrote on last edited by
          #3

          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?

          sierdzioS 1 Reply Last reply
          0
          • B BikashRDas

            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?

            sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #4

            @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).

            (Z(:^

            1 Reply Last reply
            2
            • J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #5

              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.


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              sierdzioS 1 Reply Last reply
              0
              • J.HilkJ J.Hilk

                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.

                sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by sierdzio
                #6

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

                (Z(:^

                1 Reply Last reply
                2
                • B Offline
                  B Offline
                  BikashRDas
                  wrote on last edited by
                  #7

                  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?

                  sierdzioS 1 Reply Last reply
                  0
                  • B BikashRDas

                    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?

                    sierdzioS Offline
                    sierdzioS Offline
                    sierdzio
                    Moderators
                    wrote on last edited by
                    #8

                    @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 "../".

                    (Z(:^

                    1 Reply Last reply
                    1
                    • B Offline
                      B Offline
                      BikashRDas
                      wrote on last edited by BikashRDas
                      #9

                      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.

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

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

                        (Z(:^

                        1 Reply Last reply
                        0
                        • B Offline
                          B Offline
                          BikashRDas
                          wrote on last edited by BikashRDas
                          #11

                          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

                          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