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. accessing "child" (perhaps) properties

accessing "child" (perhaps) properties

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 2 Posters 807 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.
  • S Offline
    S Offline
    shokarta
    wrote on last edited by
    #1

    Hello,

    lets say I have this:
    main.qml:

    ApplicationWindow {
        id: mainWindow
    
        StackView {
            id: view
            anchors.fill: parent
            initialItem: view_InitialScreen
        }
        Component {
            id: view_InitialScreen
            InitialScreen {}
        }
        Component.OnCompleted: { console.log(view_InitialScreen.parentObject.testRect.color); }
    }
    

    InitialScreen.qml:

    Item {
        id: parentObject
        Rectangle {
            id: testRect
            color: "white"
        }
    }
    

    so the testRect.color is obviously not accessible from main.qml component.oncompleted...
    is there a way to reach out there?

    J.HilkJ 1 Reply Last reply
    0
    • J.HilkJ J.Hilk

      @shokarta did you notice, that I removed parentObject in the console log ?

      did you do the same?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #5

      @J-Hilk said in accessing "child" (perhaps) properties:

      @shokarta did you notice, that I removed parentObject in the console log ?

      did you do the same?


      My bad, you can't access Componets that way,

      try this:

      Component.onCompleted: { console.log(view.currentItem.tRect.color); }

      with InitialItem:

      Item {
          property alias tRect: testRect
          id: parentObject
          Rectangle {
              id: testRect
              color: "white"
          }
      }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • S shokarta

        Hello,

        lets say I have this:
        main.qml:

        ApplicationWindow {
            id: mainWindow
        
            StackView {
                id: view
                anchors.fill: parent
                initialItem: view_InitialScreen
            }
            Component {
                id: view_InitialScreen
                InitialScreen {}
            }
            Component.OnCompleted: { console.log(view_InitialScreen.parentObject.testRect.color); }
        }
        

        InitialScreen.qml:

        Item {
            id: parentObject
            Rectangle {
                id: testRect
                color: "white"
            }
        }
        

        so the testRect.color is obviously not accessible from main.qml component.oncompleted...
        is there a way to reach out there?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #2

        @shokarta yes, with alias, either alias the Component or the property

        Item {
            id: parentObject
            property alias tRect: testRect
            Rectangle {
                id: testRect
                color: "white"
            }
        }
        -----
        Component.OnCompleted: { console.log(view_InitialScreen.tRect.color); }
        
        Item {
            id: parentObject
            property alias testRectColor: testRect.color
            Rectangle {
                id: testRect
                color: "white"
            }
        }
        -----
        Component.OnCompleted: { console.log(view_InitialScreen.testRectColor); }
        

        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          shokarta
          wrote on last edited by
          #3

          @J-Hilk
          Sorry, none of options work to me.

          1st gives me "qml: undefined"
          2nd gives me "qrc:/main.qml:190: TypeError: Cannot read property 'color' of undefined"

          I did set the alias property quite fine, just into Item (parentObject), but then even when I started to type view_InitialScreen.t... inside Component.OnCompleted inside ApplicationWindows (mainWindow) its not even offering me the variable tRect or restRectColor...

          J.HilkJ 1 Reply Last reply
          0
          • S shokarta

            @J-Hilk
            Sorry, none of options work to me.

            1st gives me "qml: undefined"
            2nd gives me "qrc:/main.qml:190: TypeError: Cannot read property 'color' of undefined"

            I did set the alias property quite fine, just into Item (parentObject), but then even when I started to type view_InitialScreen.t... inside Component.OnCompleted inside ApplicationWindows (mainWindow) its not even offering me the variable tRect or restRectColor...

            J.HilkJ Offline
            J.HilkJ Offline
            J.Hilk
            Moderators
            wrote on last edited by J.Hilk
            #4

            @shokarta did you notice, that I removed parentObject in the console log ?

            did you do the same?


            Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


            Q: What's that?
            A: It's blue light.
            Q: What does it do?
            A: It turns blue.

            J.HilkJ 1 Reply Last reply
            0
            • J.HilkJ J.Hilk

              @shokarta did you notice, that I removed parentObject in the console log ?

              did you do the same?

              J.HilkJ Offline
              J.HilkJ Offline
              J.Hilk
              Moderators
              wrote on last edited by
              #5

              @J-Hilk said in accessing "child" (perhaps) properties:

              @shokarta did you notice, that I removed parentObject in the console log ?

              did you do the same?


              My bad, you can't access Componets that way,

              try this:

              Component.onCompleted: { console.log(view.currentItem.tRect.color); }

              with InitialItem:

              Item {
                  property alias tRect: testRect
                  id: parentObject
                  Rectangle {
                      id: testRect
                      color: "white"
                  }
              }
              

              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              1 Reply Last reply
              0
              • S Offline
                S Offline
                shokarta
                wrote on last edited by
                #6

                @J-Hilk said in accessing "child" (perhaps) properties:

                view.currentItem

                yes, this work the way i need :) thanks
                Can I also modify it like that?

                so isntead of:

                Component.onCompleted: { console.log(view.currentItem.tRect.color); }
                

                i would do:

                Component.onCompleted: { view.currentItem.tRect.color = "red"; }
                

                should it work?

                can i also replace "currentItem" with direct id of the Component{} ? because i want not to get it from currentItem, but from "view_InitialScreen" (which ironicaly is not initial :D)

                J.HilkJ 1 Reply Last reply
                0
                • S shokarta

                  @J-Hilk said in accessing "child" (perhaps) properties:

                  view.currentItem

                  yes, this work the way i need :) thanks
                  Can I also modify it like that?

                  so isntead of:

                  Component.onCompleted: { console.log(view.currentItem.tRect.color); }
                  

                  i would do:

                  Component.onCompleted: { view.currentItem.tRect.color = "red"; }
                  

                  should it work?

                  can i also replace "currentItem" with direct id of the Component{} ? because i want not to get it from currentItem, but from "view_InitialScreen" (which ironicaly is not initial :D)

                  J.HilkJ Offline
                  J.HilkJ Offline
                  J.Hilk
                  Moderators
                  wrote on last edited by
                  #7

                  @shokarta said in accessing "child" (perhaps) properties:

                  should it work?

                  sure why not?

                  can i also replace "currentItem" with direct id of the Component{}

                  No, components are not instances, you can push the InitialItem 200 times on the stack view, how would you differentiate between them?

                  You should however be able to use the StackView methods https://doc.qt.io/qt-5/qml-qtquick-controls2-stackview.html#methods

                  to address specific items(Components) by their index


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    shokarta
                    wrote on last edited by shokarta
                    #8

                    @J-Hilk said in accessing "child" (perhaps) properties:

                    to address specific items(Components) by their index

                    why do i have to use stackview at all?
                    basicaly i wish to get the property via alias from the Item "parentObject", at the end this does not have to be bound to StackView, or does it?

                    what you are saying is exactly my problem:
                    @J-Hilk said in accessing "child" (perhaps) properties:

                    No, components are not instances, you can push the InitialItem 200 times on the stack view, how would you differentiate between them?

                    so if im pushing back and forward on each view, isnt it at the end the same view (not cloned like you mentione) just at the very top of all other views? or by each stacview.push() im cloning the view into new one and placing it atop?

                    • EDIT: i have tried to place onsole.log(currentItem) inside StackView{} to see how it will be changing, then i was clicking back and forth between two views and this is what i get:
                    qml: InitialScreen_QMLTYPE_1(0x768e90)
                    qml: ChoiseOffline_QMLTYPE_3(0xcbdd090)
                    qml: InitialScreen_QMLTYPE_1(0xcd16340)
                    qml: ChoiseOffline_QMLTYPE_3(0xce102a0)
                    qml: InitialScreen_QMLTYPE_1(0xce6b230)
                    qml: ChoiseOffline_QMLTYPE_3(0xce6b3b0)
                    qml: InitialScreen_QMLTYPE_1(0xce9af60)
                    

                    so its realy generating new index each time i push().

                    so is it posible somehow search the newest view by its name? So i wish to search InitialScreen newest index of StackView... possible somehow?

                    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