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. One single instance of a qml component used in all other qml files
Forum Updated to NodeBB v4.3 + New Features

One single instance of a qml component used in all other qml files

Scheduled Pinned Locked Moved Solved QML and Qt Quick
15 Posts 3 Posters 4.8k Views 2 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
    sierdzio
    Moderators
    wrote on 20 Dec 2017, 07:49 last edited by
    #5

    I think you don't need the qmldir, just the pragma declaration, but maybe I am mistaken. Also I don't think qmldir supports QRC paths, it should be a plain path instead.

    (Z(:^

    M 1 Reply Last reply 20 Dec 2017, 07:54
    0
    • T Tirupathi Korla
      20 Dec 2017, 07:35

      @MHermann
      Create a instance with id in main.qml and use that id in other files.
      eg.

      in main.qml..

      MyGlobalMessageBox{
         id: mGlobalMB
         ......
      }
      

      In other file

      mGlobalMB.property = xyz
      or
      mGlobalMB.functionCall()
      
      M Offline
      M Offline
      MHermann
      wrote on 20 Dec 2017, 07:49 last edited by
      #6

      @Tirupathi-Korla : Thanks for your answer.
      From my "main.qml" I am creating the sub-components dynamically.
      Therefore I can not access the ids of the "main.qml" or I don't know how to do that ...

      T 1 Reply Last reply 20 Dec 2017, 08:01
      0
      • S sierdzio
        20 Dec 2017, 07:49

        I think you don't need the qmldir, just the pragma declaration, but maybe I am mistaken. Also I don't think qmldir supports QRC paths, it should be a plain path instead.

        M Offline
        M Offline
        MHermann
        wrote on 20 Dec 2017, 07:54 last edited by
        #7

        @sierdzio : Ok. And how can I access the MessageBox? Only via it's name?

        MyGlobalMessageBox.x = ...
        ...
        

        And if am using it this way in different qml files I will always use the same instance of the MessageBox?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 20 Dec 2017, 07:56 last edited by
          #8

          Yes, it should be accessible via file name and point always to the same instance.

          (Z(:^

          M 1 Reply Last reply 20 Dec 2017, 08:02
          0
          • M MHermann
            20 Dec 2017, 07:49

            @Tirupathi-Korla : Thanks for your answer.
            From my "main.qml" I am creating the sub-components dynamically.
            Therefore I can not access the ids of the "main.qml" or I don't know how to do that ...

            T Offline
            T Offline
            Tirupathi Korla
            wrote on 20 Dec 2017, 08:01 last edited by Tirupathi Korla
            #9

            @MHermann
            As you want it as a single instance, you may create in main.qml without creating it dynamically as

            MyGlobalMessageBox{
               id: mGlobalMB
               ......
            }
            

            with which you can access it in other qml files using its id.

            M 1 Reply Last reply 20 Dec 2017, 09:02
            0
            • S sierdzio
              20 Dec 2017, 07:56

              Yes, it should be accessible via file name and point always to the same instance.

              M Offline
              M Offline
              MHermann
              wrote on 20 Dec 2017, 08:02 last edited by
              #10

              @sierdzio : Ok. At the moment I am getting no errors. The code in MyGlobalMessageBox.qml is called. But the messagebox is not shown... Maybe it is because the parent of the messagebox is null. How can I resolve this problem?

              1 Reply Last reply
              0
              • T Tirupathi Korla
                20 Dec 2017, 08:01

                @MHermann
                As you want it as a single instance, you may create in main.qml without creating it dynamically as

                MyGlobalMessageBox{
                   id: mGlobalMB
                   ......
                }
                

                with which you can access it in other qml files using its id.

                M Offline
                M Offline
                MHermann
                wrote on 20 Dec 2017, 09:02 last edited by MHermann
                #11

                @Tirupathi-Korla : Ok. I did it like you recommended.

                Main.qml:

                Page {
                   id: mainId
                
                    MyGlobalMessageBox {
                        id: myGlobMsgBox
                    }
                
                    Rectangle {
                    ...
                    GridLayout {
                        ...
                

                But I get "ReferenceError: myGlobMsgBox is not defined" when I try to access myGlobMsgBox in an other qml file. As I said before maybe it is because all other components are created dynamically in Main.qml. And it these other qml files I can not acces myGlobMsgBox neither mainId.

                T 1 Reply Last reply 20 Dec 2017, 09:07
                0
                • M MHermann
                  20 Dec 2017, 09:02

                  @Tirupathi-Korla : Ok. I did it like you recommended.

                  Main.qml:

                  Page {
                     id: mainId
                  
                      MyGlobalMessageBox {
                          id: myGlobMsgBox
                      }
                  
                      Rectangle {
                      ...
                      GridLayout {
                          ...
                  

                  But I get "ReferenceError: myGlobMsgBox is not defined" when I try to access myGlobMsgBox in an other qml file. As I said before maybe it is because all other components are created dynamically in Main.qml. And it these other qml files I can not acces myGlobMsgBox neither mainId.

                  T Offline
                  T Offline
                  Tirupathi Korla
                  wrote on 20 Dec 2017, 09:07 last edited by Tirupathi Korla
                  #12

                  @MHermann
                  May i Know whats Page?? and is Page inside any ApplicationWindow?? and what ever you create dynamically are inside Page.. Right!!!!

                  And Vetron_MessageBox is a qml file.. right!!!

                  M 1 Reply Last reply 20 Dec 2017, 09:12
                  0
                  • T Tirupathi Korla
                    20 Dec 2017, 09:07

                    @MHermann
                    May i Know whats Page?? and is Page inside any ApplicationWindow?? and what ever you create dynamically are inside Page.. Right!!!!

                    And Vetron_MessageBox is a qml file.. right!!!

                    M Offline
                    M Offline
                    MHermann
                    wrote on 20 Dec 2017, 09:12 last edited by MHermann
                    #13

                    @Tirupathi-Korla :
                    Page is loaded by a Loader in SwipeView in my MainPageManagement.qml. And this SwipeView is contained in an ApplicationWindow.
                    Yes. All dynamically created qml components are inside page.
                    I corrected my post before. I meant MyGlobalMessageBox...

                    T 1 Reply Last reply 20 Dec 2017, 09:15
                    0
                    • M MHermann
                      20 Dec 2017, 09:12

                      @Tirupathi-Korla :
                      Page is loaded by a Loader in SwipeView in my MainPageManagement.qml. And this SwipeView is contained in an ApplicationWindow.
                      Yes. All dynamically created qml components are inside page.
                      I corrected my post before. I meant MyGlobalMessageBox...

                      T Offline
                      T Offline
                      Tirupathi Korla
                      wrote on 20 Dec 2017, 09:15 last edited by
                      #14

                      @MHermann
                      Then better create Vetron_MessageBox inside ApplicationWindow.. so that it can be visible to all files..

                      M 1 Reply Last reply 20 Dec 2017, 09:53
                      0
                      • T Tirupathi Korla
                        20 Dec 2017, 09:15

                        @MHermann
                        Then better create Vetron_MessageBox inside ApplicationWindow.. so that it can be visible to all files..

                        M Offline
                        M Offline
                        MHermann
                        wrote on 20 Dec 2017, 09:53 last edited by MHermann
                        #15

                        @Tirupathi-Korla : Now I changed some things in my structure.
                        Now I can access myGlobMsgBox and the message box is working.

                        1 Reply Last reply
                        0

                        14/15

                        20 Dec 2017, 09:15

                        • Login

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