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
Forum Updated to NodeBB v4.3 + New Features

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.6k 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.
  • S Offline
    S Offline
    sush123
    wrote on 7 Apr 2020, 09:22 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
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 7 Apr 2020, 10:46 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 7 Apr 2020, 10:57
      1
      • S sierdzio
        7 Apr 2020, 10:46

        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 7 Apr 2020, 10:57 last edited by
        #3

        @sierdzio

        So using relative path would be better?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 7 Apr 2020, 11:01 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 7 Apr 2020, 12:00
          2
          • S sierdzio
            7 Apr 2020, 11:01

            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 7 Apr 2020, 12:00 last edited by
            #5

            @sierdzio

            Thanks for the suggestion

            1 Reply Last reply
            0
            • S sierdzio
              7 Apr 2020, 11:01

              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 8 Apr 2020, 09:46 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

              S 1 Reply Last reply 8 Apr 2020, 10:16
              0
              • S sush123
                8 Apr 2020, 09:46

                @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

                S Offline
                S Offline
                sierdzio
                Moderators
                wrote on 8 Apr 2020, 10:16 last edited by sierdzio 4 Aug 2020, 10:38
                #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 8 Apr 2020, 10:53 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

                  4/8

                  7 Apr 2020, 11:01

                  • Login

                  • Login or register to search.
                  4 out of 8
                  • First post
                    4/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved