
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheVisualDataModel encapsulates a model and delegateMore...
AVisualDataModel encapsulates a model and the delegate that will be instantiated for items in the model.
It is usually not necessary to createVisualDataModel elements. However, it can be useful for manipulating and accessing themodelIndex when aQAbstractItemModel subclass is used as the model. Also,VisualDataModel is used together withPackage to provide delegates to multiple views.
The example below illustrates using aVisualDataModel with aListView.
import QtQuick 1.0Rectangle {width:200;height:100VisualDataModel {id:visualModelmodel:ListModel {ListElement {name:"Apple" }ListElement {name:"Orange" } }delegate:Rectangle {height:25width:100Text {text:"Name: "+name} } }ListView {anchors.fill:parentmodel:visualModel }}
delegate :Component |
The delegate provides a template defining each item instantiated by a view. The index is exposed as an accessibleindex property. Properties of the model are also available depending upon the type ofData Model.
model :model |
This property holds the model providing data for theVisualDataModel.
The model provides a set of data that is used to create the items for a view. For large or dynamic datasets the model is usually provided by a C++ model object. The C++ model object must be aQAbstractItemModel subclass or a simple list.
Models can also be created directly in QML, using aListModel orXmlListModel.
See alsoData Models.
Theparts property selects aVisualDataModel which creates delegates from the part named. This is used in conjunction with thePackage element.
For example, the code below selects a model which creates delegates namedlist from aPackage:
VisualDataModel { id: visualModel delegate: Package { Item { Package.name:"list" } } model: myModel}ListView { width:200; height:200 model: visualModel.parts.list}See alsoPackage.
rootIndex :QModelIndex |
QAbstractItemModel provides a hierarchical tree of data, whereas QML only operates on list data.rootIndex allows the children of any node in aQAbstractItemModel to be provided by this model.
This property only affects models of typeQAbstractItemModel that are hierarchical (e.g, a tree model).
For example, here is a simple interactive file system browser. When a directory name is clicked, the view'srootIndex is set to theQModelIndex node of the clicked directory, thus updating the view to show the new directory's contents.
main.cpp:
int main(int argc,char** argv){QApplication app(argc, argv);QDeclarativeView view;QDirModel model; view.rootContext()->setContextProperty("dirModel",&model); view.setSource(QUrl::fromLocalFile("view.qml")); view.show();return app.exec();}
view.qml:
import QtQuick 1.0ListView {id:viewwidth:300height:400model:VisualDataModel {model:dirModeldelegate:Rectangle {width:200;height:25Text {text:filePath }MouseArea {anchors.fill:parentonClicked: {if (model.hasModelChildren)view.model.rootIndex=view.model.modelIndex(index) } } } }}
If themodel is aQAbstractItemModel subclass, the delegate can also reference ahasModelChildren property (optionally qualified by amodel. prefix) that indicates whether the delegate's model item has any child nodes.
See alsomodelIndex() andparentModelIndex().
QModelIndexmodelIndex(int index) |
QAbstractItemModel provides a hierarchical tree of data, whereas QML only operates on list data. This function assists in using tree models in QML.
Returns aQModelIndex for the specified index. This value can be assigned torootIndex.
See alsorootIndex.
QModelIndexparentModelIndex() |
QAbstractItemModel provides a hierarchical tree of data, whereas QML only operates on list data. This function assists in using tree models in QML.
Returns aQModelIndex for the parent of the currentrootIndex. This value can be assigned torootIndex.
See alsorootIndex.
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.