SetRootIndex in QML PathView

前端 未结 1 1111
Happy的楠姐
Happy的楠姐 2020-12-07 04:30

I\'m using a QML PathView to show my model. Such a model inherits from QStandardItemModel and has two levels of data (parent items and child items). I need to s

1条回答
  •  庸人自扰
    2020-12-07 04:48

    QML works with list models, as you have seen also in your case. However, this limitation can be easily overcome by using DelegateModel. Quoting the documentation:

    It is usually not necessary to create a DelegateModel. However, it can be useful for manipulating and accessing the modelIndex when a QAbstractItemModel subclass is used as the model. Also, DelegateModel is used together with Package to provide delegates to multiple views, and with DelegateModelGroup to sort and filter delegate items.

    Such QML type has a property rootIndex. Quoting again the documentation:

    QAbstractItemModel provides a hierarchical tree of data, whereas QML only operates on list data. rootIndex allows the children of any node in a QAbstractItemModel to be provided by this model.

    This is the property you need to set (and reset), as described in the example of the linked documentation. Note that by using DelegateModel the delegate in your PathView should not be defined. A working example (visualdatamodel/slideshow.qml) is available in the standard framework distribution under the path:

    Qt/QtXXX/Examples/Qt-5.4/quick/views
    

    Finally note that DelegateModel and VisualDataModel are often used in an interchangeable way since

    This type (VisualDataModel) is provided by the Qt QML module due to compatibility reasons. The same implementation is now primarily available as DelegateModel in the Qt QML Models module.

    0 讨论(0)
提交回复
热议问题