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.7k 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.
  • sierdzioS Offline
    sierdzioS Offline
    sierdzio
    Moderators
    wrote on last edited by
    #2

    Declare it as singleton in C++ or QML (also some info here).

    (Z(:^

    M 1 Reply Last reply
    1
    • M MHermann

      Hello!

      How is it possbile to create one single instance of a qml component (for example MyGlobalMessageBox.qml) and use this one instance in all other qml files?

      Kind regards,
      MHermann

      T Offline
      T Offline
      Tirupathi Korla
      wrote on last edited by
      #3

      @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 1 Reply Last reply
      2
      • sierdzioS sierdzio

        Declare it as singleton in C++ or QML (also some info here).

        M Offline
        M Offline
        MHermann
        wrote on last edited by
        #4

        @sierdzio : Thanks for your answer.

        Now I have done the following:

        1. qmldir:
        singleton Msgbox 1.0 qrc:/QML/Vetron_MessageBox.qml
        
        1. MyGlobalMessageBox.qml:
        pragma Singleton
        import QtQuick 2.0
        import QtQuick.Controls 2.2
        import QtQuick.Layouts 1.3
        import "."
        
        Rectangle {
        ...
        }
        

        Now a get the following runtime errors:

        QQmlApplicationEngine failed to load component
        qrc:/QML/DataHandler.qml:-1 Type Msgbox unavailable
        qrc:/QML/MyGlobalMessageBox.qml:-1 Msgbox is instantiated recursively
        

        At the moment I am not using Msgbox in the programm.
        What could be the reason for these errors?

        1 Reply Last reply
        0
        • sierdzioS Offline
          sierdzioS Offline
          sierdzio
          Moderators
          wrote on 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
          0
          • T Tirupathi Korla

            @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 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
            0
            • sierdzioS sierdzio

              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 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
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on 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
                0
                • M MHermann

                  @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 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
                  0
                  • sierdzioS sierdzio

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

                    M Offline
                    M Offline
                    MHermann
                    wrote on 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

                      @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 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
                      0
                      • M MHermann

                        @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 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
                        0
                        • T Tirupathi Korla

                          @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 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
                          0
                          • M MHermann

                            @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 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
                            0
                            • T Tirupathi Korla

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

                              M Offline
                              M Offline
                              MHermann
                              wrote on 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

                              • Login

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