Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

remove dynamically created item from a StackLayout



  • Hello,
    I am trying to delete and remove an item from a StackLayout
    I scan my stackView children to find which object to remove and then remove it (the object are dynamically created BTW using this code: component.createObject(stackView, {"device": m2});).
    But it does not work. Here is the code and the output...
    We can see that the destroy does not remove the object from it's parent, which does not seem right... And the remove from the children list does not work at all with an error. Can you help me out there?

    	  	    console.log("stackView.children.length", stackView.children.type, stackView.children.length);
                            for (i = stackView.children.length; --i>=0; )
                                if ("device" in stackView.children[i])
                                    if ("deleted" in stackView.children[i].device)
                                        if (stackView.children[i].device.deleted)
                                        {
    					console.log("removing item1", i, stackView.children.length);
                                              stackView.children[i].destroy(); 
    					console.log("removing item2", i, stackView.children.length);
    					stackView.children.remove(i);
    					console.log("removed item3", i, stackView.children.length);
                                        }
    

    qml: stackView.children.length undefined 6
    qml: removing item1 3 6
    qml: removing item2 3 6
    qrc:/qml/main.qml:388: TypeError: Property 'remove' of object [object Object] is not a function

    Thanks



  • First of all, why don't you push items to a StackView via push method? StackView will automatically create such items and own them, this means, that when you poped out such item it will be automatically destroyed. If you want to create items dynamically and push them into view, then you must pop them and then call destroy on such item.


  • Qt Champions 2018

    That seems overly complicated.
    I would recommend you to use a model instead, and a Repeater inside your StackLayout.

    One good rule to apply is to modify data and let the UI reacts to those data changes, instead of directly modify the UI.


Log in to reply