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



  • 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!



  • 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
    @



  • 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 ;)



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



  • 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();
    }
    @



  • 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?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.