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. remove dynamically created item from a StackLayout
Forum Update on Monday, May 27th 2025

remove dynamically created item from a StackLayout

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 3 Posters 1.3k 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.
  • C Offline
    C Offline
    Cyrille de Brebisson
    wrote on last edited by Cyrille de Brebisson
    #1

    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

    1 Reply Last reply
    0
    • IntruderExcluderI Offline
      IntruderExcluderI Offline
      IntruderExcluder
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • GrecKoG Offline
        GrecKoG Offline
        GrecKo
        Qt Champions 2018
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved