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. including qml file from another folder
Forum Update on Monday, May 27th 2025

including qml file from another folder

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
19 Posts 5 Posters 2.0k 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.
  • M Offline
    M Offline
    mzimmers
    wrote on 27 Mar 2024, 23:31 last edited by
    #1

    Hi all -

    I've been breaking up a code base into multiple folders/sub-folders, more for organizational clarity than anything. It's all working, except for one file, located at:

    <mainFolder>\app\qml\equipment\components\FileA.qml
    

    which is trying to use another file located at:

    <mainFolder>\qmlplugins\Nga\Components\DrawerList.qml
    

    with this line:

    equipmentDrawerStack.push("DrawerList.qml",
    

    At run time, my app is looking in the first folder (app\qml\equipment\components) and not finding the file.

    I realize I can hard-code a relative pathname, but I'd prefer not to do that. Can someone suggest a better method for this?

    Thanks....

    J 1 Reply Last reply 28 Mar 2024, 06:33
    0
    • M mzimmers
      27 Mar 2024, 23:31

      Hi all -

      I've been breaking up a code base into multiple folders/sub-folders, more for organizational clarity than anything. It's all working, except for one file, located at:

      <mainFolder>\app\qml\equipment\components\FileA.qml
      

      which is trying to use another file located at:

      <mainFolder>\qmlplugins\Nga\Components\DrawerList.qml
      

      with this line:

      equipmentDrawerStack.push("DrawerList.qml",
      

      At run time, my app is looking in the first folder (app\qml\equipment\components) and not finding the file.

      I realize I can hard-code a relative pathname, but I'd prefer not to do that. Can someone suggest a better method for this?

      Thanks....

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 28 Mar 2024, 06:33 last edited by
      #2

      @mzimmers said in including qml file from another folder:

      I realize I can hard-code a relative pathname, but I'd prefer not to do that

      Why not?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply 28 Mar 2024, 13:29
      0
      • J jsulm
        28 Mar 2024, 06:33

        @mzimmers said in including qml file from another folder:

        I realize I can hard-code a relative pathname, but I'd prefer not to do that

        Why not?

        M Offline
        M Offline
        mzimmers
        wrote on 28 Mar 2024, 13:29 last edited by
        #3

        @jsulm because I'm hoping there's a better way, either with some Qt facility or some CMake wizardry that I don't know about.

        J 1 Reply Last reply 28 Mar 2024, 13:39
        0
        • M mzimmers
          28 Mar 2024, 13:29

          @jsulm because I'm hoping there's a better way, either with some Qt facility or some CMake wizardry that I don't know about.

          J Offline
          J Offline
          JoeCFD
          wrote on 28 Mar 2024, 13:39 last edited by JoeCFD
          #4

          @mzimmers take a look at alias in qrc
          https://doc.qt.io/qt-6/resources.html

          M 1 Reply Last reply 28 Mar 2024, 15:09
          0
          • J JoeCFD
            28 Mar 2024, 13:39

            @mzimmers take a look at alias in qrc
            https://doc.qt.io/qt-6/resources.html

            M Offline
            M Offline
            mzimmers
            wrote on 28 Mar 2024, 15:09 last edited by mzimmers
            #5

            @JoeCFD there's a complicating factor here. This is my project structure:
            Screenshot 2024-03-28 080339.png
            The file to be included is in qmlplugins/Nga/Components. The file trying to include it is in appNgaIcdFw/appNgaIcdFw/qml/equipment/components.

            All of my other files in appNgaIcdFw can find the files in Nga/Components. For some reason, though, it can't find this new one. The new file is in the CMakeLists.txt file, so it should be treated like all the others.

            This is the error I get:

            qrc:/qt/qml/NgaIcdFw/qml/equipment/components/DrawerOpener.qml:79:9: QML StackView: push: qrc:/qt/qml/NgaIcdFw/qml/equipment/components/Timepicker.qml:-1 No such file or directory
            
            J A 2 Replies Last reply 28 Mar 2024, 15:21
            0
            • M mzimmers
              28 Mar 2024, 15:09

              @JoeCFD there's a complicating factor here. This is my project structure:
              Screenshot 2024-03-28 080339.png
              The file to be included is in qmlplugins/Nga/Components. The file trying to include it is in appNgaIcdFw/appNgaIcdFw/qml/equipment/components.

              All of my other files in appNgaIcdFw can find the files in Nga/Components. For some reason, though, it can't find this new one. The new file is in the CMakeLists.txt file, so it should be treated like all the others.

              This is the error I get:

              qrc:/qt/qml/NgaIcdFw/qml/equipment/components/DrawerOpener.qml:79:9: QML StackView: push: qrc:/qt/qml/NgaIcdFw/qml/equipment/components/Timepicker.qml:-1 No such file or directory
              
              J Offline
              J Offline
              JoeCFD
              wrote on 28 Mar 2024, 15:21 last edited by
              #6

              @mzimmers delete everything in the build dir and rebuild the whole project.

              M 1 Reply Last reply 28 Mar 2024, 15:31
              0
              • J JoeCFD
                28 Mar 2024, 15:21

                @mzimmers delete everything in the build dir and rebuild the whole project.

                M Offline
                M Offline
                mzimmers
                wrote on 28 Mar 2024, 15:31 last edited by mzimmers
                #7

                @JoeCFD same problem.

                EDIT: the problem is more general than I first realized. I can't use any of the files in the plugins directory structure.

                J 1 Reply Last reply 28 Mar 2024, 18:10
                0
                • M mzimmers
                  28 Mar 2024, 15:31

                  @JoeCFD same problem.

                  EDIT: the problem is more general than I first realized. I can't use any of the files in the plugins directory structure.

                  J Offline
                  J Offline
                  johngod
                  wrote on 28 Mar 2024, 18:10 last edited by
                  #8

                  @mzimmers Hi

                  In your FileA.qml, where you want to use the stuff from DrawerList.qml do the following:

                  import "../../../../qmlplugins/NgaComponents"
                  

                  Note I may have miscounted the number ../ in the import, but you get the picture :)

                  M 1 Reply Last reply 28 Mar 2024, 20:27
                  0
                  • M mzimmers
                    28 Mar 2024, 15:09

                    @JoeCFD there's a complicating factor here. This is my project structure:
                    Screenshot 2024-03-28 080339.png
                    The file to be included is in qmlplugins/Nga/Components. The file trying to include it is in appNgaIcdFw/appNgaIcdFw/qml/equipment/components.

                    All of my other files in appNgaIcdFw can find the files in Nga/Components. For some reason, though, it can't find this new one. The new file is in the CMakeLists.txt file, so it should be treated like all the others.

                    This is the error I get:

                    qrc:/qt/qml/NgaIcdFw/qml/equipment/components/DrawerOpener.qml:79:9: QML StackView: push: qrc:/qt/qml/NgaIcdFw/qml/equipment/components/Timepicker.qml:-1 No such file or directory
                    
                    A Offline
                    A Offline
                    afalsa
                    wrote on 28 Mar 2024, 19:24 last edited by
                    #9

                    @mzimmers said in including qml file from another folder:

                    Timepicker

                    Is DrawerList.qml added to any qmldir inside qmlplugins/Nga/Components?

                    M 1 Reply Last reply 28 Mar 2024, 19:51
                    0
                    • A afalsa
                      28 Mar 2024, 19:24

                      @mzimmers said in including qml file from another folder:

                      Timepicker

                      Is DrawerList.qml added to any qmldir inside qmlplugins/Nga/Components?

                      M Offline
                      M Offline
                      mzimmers
                      wrote on 28 Mar 2024, 19:51 last edited by
                      #10

                      @afalsa I don't have any qmldir files anywhere in my project.

                      A 1 Reply Last reply 29 Mar 2024, 08:09
                      0
                      • J johngod
                        28 Mar 2024, 18:10

                        @mzimmers Hi

                        In your FileA.qml, where you want to use the stuff from DrawerList.qml do the following:

                        import "../../../../qmlplugins/NgaComponents"
                        

                        Note I may have miscounted the number ../ in the import, but you get the picture :)

                        M Offline
                        M Offline
                        mzimmers
                        wrote on 28 Mar 2024, 20:27 last edited by
                        #11

                        @johngod you didn't miscount, though I think you meant:

                        import "../../../../qmlplugins/Nga/Components"
                        

                        But I don't think this is necessary. I already have a line:

                        import Nga.Components
                        

                        It may be noteworthy that if I directly reference DrawerList from the same file, that works. It's the argument to the push() that seems to need some pathname, though I'd like to avoid having to do this.

                        J 1 Reply Last reply 28 Mar 2024, 23:09
                        0
                        • M mzimmers
                          28 Mar 2024, 20:27

                          @johngod you didn't miscount, though I think you meant:

                          import "../../../../qmlplugins/Nga/Components"
                          

                          But I don't think this is necessary. I already have a line:

                          import Nga.Components
                          

                          It may be noteworthy that if I directly reference DrawerList from the same file, that works. It's the argument to the push() that seems to need some pathname, though I'd like to avoid having to do this.

                          J Offline
                          J Offline
                          johngod
                          wrote on 28 Mar 2024, 23:09 last edited by johngod
                          #12

                          @mzimmers Are you using a resource file to store your qml files ? I usually do that, then in the in project editor, I expand the resource file, select the qml file I want to use, right click on the file, there is one option to Copy the Path, something like ":/someFolder/SomeFile.qml" and other that is Copy Url, something like "qrc:/someFolder/SomeFile.qml". One of this options should work, dont remember right now which is the correct. Edit: this are relative paths to the qml files inside the resource files, this is a good approach imho, I fail to see why you would not want to use this relative paths.

                          M 1 Reply Last reply 29 Mar 2024, 15:59
                          0
                          • M mzimmers
                            28 Mar 2024, 19:51

                            @afalsa I don't have any qmldir files anywhere in my project.

                            A Offline
                            A Offline
                            afalsa
                            wrote on 29 Mar 2024, 08:09 last edited by
                            #13

                            @mzimmers

                            Normally when i have these type of errors it is because is not well referenced in a qmldir file

                            Timepicker.qml:-1 No such file or directory
                            
                            1 Reply Last reply
                            0
                            • J johngod
                              28 Mar 2024, 23:09

                              @mzimmers Are you using a resource file to store your qml files ? I usually do that, then in the in project editor, I expand the resource file, select the qml file I want to use, right click on the file, there is one option to Copy the Path, something like ":/someFolder/SomeFile.qml" and other that is Copy Url, something like "qrc:/someFolder/SomeFile.qml". One of this options should work, dont remember right now which is the correct. Edit: this are relative paths to the qml files inside the resource files, this is a good approach imho, I fail to see why you would not want to use this relative paths.

                              M Offline
                              M Offline
                              mzimmers
                              wrote on 29 Mar 2024, 15:59 last edited by
                              #14

                              @johngod said in including qml file from another folder:

                              Are you using a resource file to store your qml files ?

                              No, I'm not -- that hasn't been necessary so far. If I decide to do this, would it be best to create it at the top project level, so that it covers all the files referenced in the contained CMake files, or should I create it at the /qml/Components level? Or, does it matter?

                              J 1 Reply Last reply 30 Mar 2024, 04:52
                              0
                              • M mzimmers
                                29 Mar 2024, 15:59

                                @johngod said in including qml file from another folder:

                                Are you using a resource file to store your qml files ?

                                No, I'm not -- that hasn't been necessary so far. If I decide to do this, would it be best to create it at the top project level, so that it covers all the files referenced in the contained CMake files, or should I create it at the /qml/Components level? Or, does it matter?

                                J Offline
                                J Offline
                                johngod
                                wrote on 30 Mar 2024, 04:52 last edited by
                                #15

                                @mzimmers You have to use it at top level, since you cannot add files at a upper level folder, then you can add all the qml files of your projet. Using a resource file for your qml files will make them a part of the excutable file, you will not have to worry about deploy them.

                                M 1 Reply Last reply 1 Apr 2024, 15:32
                                0
                                • J johngod
                                  30 Mar 2024, 04:52

                                  @mzimmers You have to use it at top level, since you cannot add files at a upper level folder, then you can add all the qml files of your projet. Using a resource file for your qml files will make them a part of the excutable file, you will not have to worry about deploy them.

                                  M Offline
                                  M Offline
                                  mzimmers
                                  wrote on 1 Apr 2024, 15:32 last edited by
                                  #16

                                  @johngod thanks for the reply. I'm still not sure I understand where I should put my resources file. My project structure looks like this:
                                  Screenshot 2024-04-01 083020.png
                                  If I put the file under qmlplugins, it won't be visible to appNgaIcdFw, will it?

                                  J 1 Reply Last reply 1 Apr 2024, 15:51
                                  0
                                  • M mzimmers
                                    1 Apr 2024, 15:32

                                    @johngod thanks for the reply. I'm still not sure I understand where I should put my resources file. My project structure looks like this:
                                    Screenshot 2024-04-01 083020.png
                                    If I put the file under qmlplugins, it won't be visible to appNgaIcdFw, will it?

                                    J Offline
                                    J Offline
                                    JoeCFD
                                    wrote on 1 Apr 2024, 15:51 last edited by JoeCFD 4 Jan 2024, 16:00
                                    #17

                                    @mzimmers I guess it may be better to put shared qml files into a dir like for example: common or general or shared for both project and plugin. I think you may have some base qml files as well for both projects and they may belong to this dir too.
                                    In this way, both projects will use the exactly same path in their own cmake files.

                                    M 1 Reply Last reply 4 Apr 2024, 16:22
                                    0
                                    • J JoeCFD
                                      1 Apr 2024, 15:51

                                      @mzimmers I guess it may be better to put shared qml files into a dir like for example: common or general or shared for both project and plugin. I think you may have some base qml files as well for both projects and they may belong to this dir too.
                                      In this way, both projects will use the exactly same path in their own cmake files.

                                      M Offline
                                      M Offline
                                      mzimmers
                                      wrote on 4 Apr 2024, 16:22 last edited by
                                      #18

                                      @JoeCFD My top-level CMakeLists.txt file already has these entries:

                                      add_subdirectory(qmlplugins)
                                      add_subdirectory(appNgaIcdFw)
                                      

                                      So I think the qmlplugins directory is already doing what you suggested. I think @johngod is correct about me needing a resources file, but I don't know where to place it for it to be visible to everything.

                                      J 1 Reply Last reply 5 Apr 2024, 10:20
                                      0
                                      • M mzimmers
                                        4 Apr 2024, 16:22

                                        @JoeCFD My top-level CMakeLists.txt file already has these entries:

                                        add_subdirectory(qmlplugins)
                                        add_subdirectory(appNgaIcdFw)
                                        

                                        So I think the qmlplugins directory is already doing what you suggested. I think @johngod is correct about me needing a resources file, but I don't know where to place it for it to be visible to everything.

                                        J Offline
                                        J Offline
                                        johngod
                                        wrote on 5 Apr 2024, 10:20 last edited by
                                        #19

                                        @mzimmers Hi
                                        I usually would put my resource in the top level directory ngalcdFwProj, but I think you can also put it in qmlplugins and it still would be visible appNgaIcdFw, give it a try. Note that is this last case, you will only be able to add qml files from the qmlplugins and the sub folders to the resource. Note also that you can add several resource files, for example, one for the qmlplugins folder, other for appNgalcdFw, and so on.

                                        1 Reply Last reply
                                        0

                                        1/19

                                        27 Mar 2024, 23:31

                                        • Login

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