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 pass the data from main qml to another qml and vise versa.
QtWS25 Last Chance

how to pass the data from main qml to another qml and vise versa.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
8 Posts 4 Posters 3.1k 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.
  • V Offline
    V Offline
    venkatesh88
    wrote on last edited by venkatesh88
    #1

    hi i am new to qt qml . i have created Comp1.qml with two texts and this Comp1.qml is accessing from the main.qml . what i want to do is i want change the text value which is in Comp1.qml from the main.qml . can any one please help1_1541485244630_main.qml.png 0_1541485244628_Comp1.png

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      No need to add the Text again in side the comp.

      Comp1 {
      id : obj
      aString:"Venki"
      }

      Based on some action inside the main.qml object
      just specify obj.aString = "PthinkS"

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      1
      • V Offline
        V Offline
        venkatesh88
        wrote on last edited by
        #3

        thanks worked , but is it possible to access the variables main.qml from Comp1.qml . i mean opposite of above case

        E Gojir4G 2 Replies Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          You can. Not a good practice.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • E Offline
            E Offline
            Eeli K
            wrote on last edited by
            #5

            Does this old post answer your question? https://forum.qt.io/topic/74407/how-to-enter-different-stackviews/7

            This is really very basic understanding about how qml files and objects work, kind of a FAQ.

            1 Reply Last reply
            0
            • V venkatesh88

              thanks worked , but is it possible to access the variables main.qml from Comp1.qml . i mean opposite of above case

              E Offline
              E Offline
              Eeli K
              wrote on last edited by
              #6

              @venkatesh88 said in how to pass the data from main qml to another qml and vise versa.:

              but is it possible to access the variables main.qml from Comp1.qml . i mean opposite of above case

              Like dheerendra said, not recommended. If you write a component (Comp1) it's better to do it so that it's not dependent of its surroundings. You can actually access the "parent" property from the main level object of your component but then you have the risk of using it in a way which can't be done run time. For example, if you write in your component:

              parent.xyzzy = 1
              

              you have to instantiate your component only inside a parent which actually has the property "xyzzy". Otherwise you will get a runtime error. In C++ you would give the "parent" as an argument when you create or use the "component" but there you have compile time type checking.

              1 Reply Last reply
              0
              • E Offline
                E Offline
                Eeli K
                wrote on last edited by
                #7

                Here's a similar problem and possible answers: https://forum.qt.io/topic/82459/qml-scope-how-to-access-objects

                1 Reply Last reply
                0
                • V venkatesh88

                  thanks worked , but is it possible to access the variables main.qml from Comp1.qml . i mean opposite of above case

                  Gojir4G Offline
                  Gojir4G Offline
                  Gojir4
                  wrote on last edited by
                  #8

                  @venkatesh88 Hi,

                  Use a signal from Comp1 to notify the parent that the value has changed and then update it from the parent side.
                  Also you could use alias instead of redefining properties

                  //Comp1.qml
                  MainItem{
                      property alias aString: mvalue.text
                      property alias maxValue: maxvalue.text 
                      // ... Comp1 definition
                  }
                  //main.qml
                  
                  Window{
                      ....  
                      Comp1{
                          ...
                          onMaxValueChanged: xxyyzz = maxValue
                      }
                  }
                  
                  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