VisualDataModel Element Reference

The VisualDataModel encapsulates a model and delegate

Detailed Description

The VisualDataModel encapsulates a model and delegate

A VisualDataModel encapsulates a model and the delegate that will be instantiated for items in the model.

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

The example below illustrates using a VisualDataModel with a ListView.

  1. import QtQuick 1.0
  2.  
  3. Rectangle  {
  4.     width: 200; height: 100
  5.  
  6.     VisualDataModel  {
  7.         id: visualModel
  8.         model: ListModel  {
  9.             ListElement  { name: "Apple" }
  10.             ListElement  { name: "Orange" }
  11.         }
  12.         delegate: Rectangle  {
  13.             height: 25
  14.             width: 100
  15.             Text  { text: "Name: " + name}
  16.         }
  17.     }
  18.  
  19.     ListView  {
  20.         anchors.fill: parent
  21.         model: visualModel
  22.     }
  23. }

Properties

Methods

Property Documentation

The delegate provides a template defining each item instantiated by a view. The index is exposed as an accessible index property. Properties of the model are also available depending upon the type of Data Model.

This property holds the model providing data for the VisualDataModel.

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 a QAbstractItemModel subclass or a simple list.

Models can also be created directly in QML, using a ListModel or XmlListModel.

See also Data Models.

  • parts : object

The parts property selects a VisualDataModel which creates delegates from the part named. This is used in conjunction with the Package element.

For example, the code below selects a model which creates delegates named list from a Package:

  1. VisualDataModel  {
  2.     id: visualModel
  3.     delegate: Package  {
  4.         Item  { Package.name: "list" }
  5.     }
  6.     model: myModel
  7. }
  8.  
  9. ListView  {
  10.     width: 200; height:200
  11.     model: visualModel.parts.list
  12. }

See also Package.

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 property only affects models of type QAbstractItemModel 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's rootIndex is set to the QModelIndex node of the clicked directory, thus updating the view to show the new directory's contents.

main.cpp:

  1. int main(int argc, char ** argv)
  2.  {
  3.     QApplication app(argc, argv);
  4.  
  5.     QDeclarativeView view;
  6.  
  7.     QDirModel model;
  8.     view.rootContext()->setContextProperty("dirModel", &model);
  9.  
  10.     view.setSource(QUrl::fromLocalFile("view.qml"));
  11.     view.show();
  12.  
  13.     return app.exec();
  14. }

view.qml:

  1. import QtQuick 1.0
  2.  
  3. ListView  {
  4.     id: view
  5.     width: 300
  6.     height: 400
  7.  
  8.     model: VisualDataModel  {
  9.         model: dirModel
  10.  
  11.         delegate: Rectangle  {
  12.             width: 200; height: 25
  13.             Text  { text: filePath }
  14.  
  15.             MouseArea  {
  16.                 anchors.fill: parent
  17.                 onClicked:  {
  18.                     if (model.hasModelChildren)
  19.                         view.model.rootIndex = view.model.modelIndex(index)
  20.                 }
  21.             }
  22.         }
  23.     }
  24. }

If the model is a QAbstractItemModel subclass, the delegate can also reference a hasModelChildren property (optionally qualified by a model. prefix) that indicates whether the delegate's model item has any child nodes.

See also modelIndex() and parentModelIndex().

Method Documentation

QAbstractItemModel provides a hierarchical tree of data, whereas QML only operates on list data. This function assists in using tree models in QML.

Returns a QModelIndex for the specified index. This value can be assigned to rootIndex.

See also rootIndex.

QAbstractItemModel provides a hierarchical tree of data, whereas QML only operates on list data. This function assists in using tree models in QML.

Returns a QModelIndex for the parent of the current rootIndex. This value can be assigned to rootIndex.

See also rootIndex.

Notes provided by the Qt Community

No notes