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. Get all the children form an element, even the "outerly added children"
Forum Updated to NodeBB v4.3 + New Features

Get all the children form an element, even the "outerly added children"

Scheduled Pinned Locked Moved QML and Qt Quick
6 Posts 2 Posters 1.5k Views 1 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.
  • E Offline
    E Offline
    excalibur1491
    wrote on last edited by
    #1

    Hi,
    I am doing a little project only consisting in a button (that I will use later in another project) and I have a problem:
    The code is here: https://github.com/ddeunagomez/qmlbutton , check Button.qml and main.qml.

    The code is faily simple, the problem is :
    My buttons don't have Icons by default, but it very easy to add them by doing the following:
    @ Button{
    x: 10
    color: "grey"
    animate: true
    Image {
    anchors.centerIn: parent
    scale : 0.2
    source: "qt-logo.svg"
    }
    }@
    The problem is that I don't find any way to get that Image as a child of the Button inside the Button.
    What I mean is that if inside the button code I do children, then children doesn't contain that Image.
    @MouseArea {
    anchors.fill: parent
    onClicked: parent.clicked();
    onPressed: {
    if (parent.animate)
    //button.y += 2
    for (var i = 0; i < parent.children.length; i++){
    if (parent.children[i] != shadow)
    parent.children[i].y +=2
    }
    parent.pressed();
    }
    onReleased: {
    if (parent.animate)
    //button.y -= 2
    for (var i = 0; i < parent.children.length; i++){
    if (parent.children[i] != shadow)
    parent.children[i].y -=2
    }
    parent.released();
    }
    onPressAndHold: parent.pressAndHold();
    onDoubleClicked: parent.doubleClicked();
    }@
    In the loop at line 7 I try to get all the children to move them slightly, but the Image won't move because it's not among the children. All the other elements move correctly.

    So, how do I get the elements that have been added byt the user to my Item?

    Thank you!

    1 Reply Last reply
    0
    • X Offline
      X Offline
      Xander84
      wrote on last edited by
      #2

      Hey, I am wondering about your condition in the for loop, what is that?
      @
      i > parent.children
      @
      i would do
      @
      i < parent.children.length
      @
      since parent.children is just the list, how does that event work? :D

      similar with
      @
      parent.children.y +=2
      @
      you need to specify the index!?
      @
      parent.children[i].y +=2
      @

      1 Reply Last reply
      0
      • E Offline
        E Offline
        excalibur1491
        wrote on last edited by
        #3

        Oh, so sorry, I did a lot of undo's to clean the code beffore pushing it to my repo nd I didn't see I mesed up the code..
        Sorry, obviosuly the loop code is:

        @for (var i = 0; i < parent.children.length; i++){
        if (parent.children[i] != shadow)
        parent.children[i].y +=2
        }@

        I updated my first post ;)

        1 Reply Last reply
        0
        • X Offline
          X Offline
          Xander84
          wrote on last edited by
          #4

          You are still missing the length property?
          parent.children is a list or array and you are comparing that to 0.

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

            Oh! So sorry! Ok, now I corrected the code in the post and runed it.
            So here is my MouseArea.
            You can try it, this only moves the children that are dlecared in Button.qml, not the ones added to the button on main.qml when using the button.
            @
            MouseArea {
            anchors.fill: parent
            onClicked: parent.clicked();
            onPressed: {
            if (parent.animate)
            //button.y += 2
            for (var i = 0; i < parent.children.length; i++){
            if (parent.children[i] != shadow)
            parent.children[i].y +=2
            }
            parent.pressed();
            }
            onReleased: {
            if (parent.animate)
            //button.y -= 2
            for (var i = 0; i < parent.children.length; i++){
            if (parent.children[i] != shadow)
            parent.children[i].y -=2
            }
            parent.released();
            }
            onPressAndHold: parent.pressAndHold();
            onDoubleClicked: parent.doubleClicked();
            }
            @

            1 Reply Last reply
            0
            • X Offline
              X Offline
              Xander84
              wrote on last edited by
              #6

              Maybe the Items added to the Button in you main.qml are not added to the button directly but to a new Item, I am not sure how QML works if you create objects from QML files but you might create a new derived Item and that is why the other Items are not found in your loop?

              so you can maybe try the parent of you button?
              try and use the QML debugger to see the item tree or print this to the console and see for yourself:
              @
              console.log(parent.children.length, parent.parent.children.length)
              @
              maybe you need to also use the parent.parent.children list?

              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