2

I'm looking for code sample to collapse/expand ListItem in QML.

Currently I have an idea to do it through toggling visible flag for each child of triggered item. But I can't imagine how to iterate the children. Here is piece of code:

        ListView {
            dataModel: XmlDataModel {...}
            onTriggered: {
                var dataType = dataModel.itemType(indexPath)
                if (dataType == "header")
                {
                    var childrenCount = dataModel.childCount(indexPath);
                    for (int i = 0; i < childrenCount; i++)
                    {
                        // what to write here to get each child ListItem?
                    }
                }
            }
        }

Thanks in advance.

PS: There is a sample for my task where we inherit from bb::cascades::DataModel and override childCount. But I would like to not deal with C++ classes.

brigadir
  • 6,874
  • 6
  • 46
  • 81

1 Answers1

1

Perhaps you could consider setting a visible property in the dataModel and binding the visibility of the children to it.

ListView {
    dataModel: XmlDataModel {...}
...
    listItemComponents: [
        ListItemComponent {
            visible: ListItemData.visible
...
        }
    ]
}

Then to loop through the children and make them all invisible you could just loop through the dataModel.

Konrad Lindenbach
  • 4,911
  • 1
  • 26
  • 28
  • Yes, it can be an approach. Little disadvantage that we need to keep visual property in model structure. I'll wait for possible answer showing how to iterate `ListItem` children. – brigadir May 23 '14 at 05:53
  • This has come up before around SO. I don't believe you can iterate through ListView children/ – Konrad Lindenbach May 23 '14 at 12:44
  • In this case it would be nicer to map `visible` property of each child to `expanded` property of its parent. Can we access parent in `ListItemData `? – brigadir May 23 '14 at 13:55
  • If you are asking about binding listItemComponents to properties of their parent list view see my answer [here](http://stackoverflow.com/questions/18375182/accessing-listitemcomponents-on-the-run) – Konrad Lindenbach May 23 '14 at 15:06
  • In my case parent is also `ListItemComponent` and I would like to bind it's property to children's `visible` flag. Still can't find how to access the parent list item. – brigadir May 28 '14 at 17:29