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. How to import qml file in another qml file without using relative path
QtWS25 Last Chance

How to import qml file in another qml file without using relative path

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
8 Posts 2 Posters 2.5k Views
  • 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.
  • S Offline
    S Offline
    sush123
    wrote on last edited by
    #1

    Hi All,

    My file structure is :

    C:\Users\Documents\project\source\qml\Common

    Common folder has :
    Toolbar.qml
    Menubutton.qml

    C:\Users\Documents\project\source\qml\Views

    Views folder has:
    Welcomscreen.qml

    So i need to import Toolbar.qml in Welcomscreen.qml right now i am using import "../Common/" which is working but i do not want to use relative path , can any one help me is there any way i can use import "qrc:qml/Common",

    i tried to have alias name that also did not help me.

    Thanks in advance :)

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

      As mentioned in chat - I don't know of any built-in solution to this.

      There is one thing that will work - add qmldir file to your Common dir to transform it into a QML module. Then you can import with something like import Common 1.0.

      The big disadvantage of this is that qmldir file has to be maintained and quite frankly it is cumbersome.

      (Z(:^

      S 1 Reply Last reply
      1
      • sierdzioS sierdzio

        As mentioned in chat - I don't know of any built-in solution to this.

        There is one thing that will work - add qmldir file to your Common dir to transform it into a QML module. Then you can import with something like import Common 1.0.

        The big disadvantage of this is that qmldir file has to be maintained and quite frankly it is cumbersome.

        S Offline
        S Offline
        sush123
        wrote on last edited by
        #3

        @sierdzio

        So using relative path would be better?

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

          Easier. Whether it's better or not depends highly on specifics of your project. If you move files a lot, then qmldir can be more convenient (no need to update any paths!). If you instead add to Common dir files often, you will have to keep modifying your .qrc file and your qmldir, which is annoying.

          There is no clear answer IMO.

          (Z(:^

          S 2 Replies Last reply
          2
          • sierdzioS sierdzio

            Easier. Whether it's better or not depends highly on specifics of your project. If you move files a lot, then qmldir can be more convenient (no need to update any paths!). If you instead add to Common dir files often, you will have to keep modifying your .qrc file and your qmldir, which is annoying.

            There is no clear answer IMO.

            S Offline
            S Offline
            sush123
            wrote on last edited by
            #5

            @sierdzio

            Thanks for the suggestion

            1 Reply Last reply
            0
            • sierdzioS sierdzio

              Easier. Whether it's better or not depends highly on specifics of your project. If you move files a lot, then qmldir can be more convenient (no need to update any paths!). If you instead add to Common dir files often, you will have to keep modifying your .qrc file and your qmldir, which is annoying.

              There is no clear answer IMO.

              S Offline
              S Offline
              sush123
              wrote on last edited by
              #6

              @sierdzio

              i have created a qmldir inside Common folder , can you please help me to add this module to access import Common 1.0 in other qml files.

              where i should add this module ?

              Do i need to add qmldir to my qrc file?

              Do i need to engine.addImportPath("qrc:/qml/Common/qmldir"); in main.cpp

              thanks

              sierdzioS 1 Reply Last reply
              0
              • S sush123

                @sierdzio

                i have created a qmldir inside Common folder , can you please help me to add this module to access import Common 1.0 in other qml files.

                where i should add this module ?

                Do i need to add qmldir to my qrc file?

                Do i need to engine.addImportPath("qrc:/qml/Common/qmldir"); in main.cpp

                thanks

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

                @sush123 said in How to import qml file in another qml file without using relative path:

                @sierdzio

                i have created a qmldir inside Common folder , can you please help me to add this module to access import Common 1.0 in other qml files.

                https://doc.qt.io/qt-5/qtqml-modules-qmldir.html

                Your qmldir should be something like:

                module ModuleName
                singleton SomeSingleTonFile 1.0 SomeSingleTonFile.qml
                SomeOtherFile 1.0 SomeOtherFile.qml
                

                And then in QML:

                import ModuleName 1.0
                // ...
                SomeOtherFile {
                  // it works!
                }
                

                where i should add this module ?

                Do i need to add qmldir to my qrc file?

                Yes. Both the dir and all of the files inside need to be in QRC.

                Do i need to engine.addImportPath("qrc:/qml/Common/qmldir"); in main.cpp

                No.

                (Z(:^

                1 Reply Last reply
                1
                • S Offline
                  S Offline
                  sush123
                  wrote on last edited by
                  #8

                  thanks for the reply ,

                  But i did try the same steps but still i am getting "Qml module not found error".

                  sample of qmldir

                  module GUI.Source.Qml.Common

                  Sample 1.0 Sample.qml

                  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