![]() |
Home · All Classes · All Functions · Overviews |
The VisualDataModel encapsulates a model and delegate More...
A VisualDataModel encapsulates a model and the delegate that will be instantiated for items in the model.
It is usually not necessary to create a VisualDataModel directly, since the QML views will create one internally.
The example below illustrates using a VisualDataModel with a ListView.
VisualDataModel {
id: visualModel
model: myModel
delegate: Component {
Rectangle {
height: 25
width: 100
Text { text: "Name:" + name}
}
}
}
ListView {
width: 100
height: 100
anchors.fill: parent
model: visualModel
}
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.
Here is an example delegate:
Component {
id: delegate
Item {
id: wrapper
width: 180; height: 40
Column {
x: 5; y: 5
Text { text: '<b>Name:</b> ' + name }
Text { text: '<b>Number:</b> ' + number }
}
}
}
| model : 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.
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:
VisualDataModel {
id: visualModel
delegate: Package {
Item { Package.name: "list" }
}
model: myModel
}
ListView {
width: 200; height:200
model: visualModel.parts.list
}
See also Package.
| rootIndex : QModelIndex |
QAbstractItemModel provides a heirachical 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.
// main.cpp
Q_DECLARE_METATYPE(QModelIndex)
class MyModel : public QDirModel
{
Q_OBJECT
public:
MyModel(QDeclarativeContext *ctxt) : QDirModel(), context(ctxt) {
QHash<int,QByteArray> roles = roleNames();
roles.insert(FilePathRole, "path");
setRoleNames(roles);
context->setContextProperty("myModel", this);
context->setContextProperty("myRoot", QVariant::fromValue(index(0,0,QModelIndex())));
}
Q_INVOKABLE void setRoot(const QString &path) {
QModelIndex root = index(path);
context->setContextProperty("myRoot", QVariant::fromValue(root));
}
QDeclarativeContext *context;
};
int main(int argc, char ** argv)
{
QApplication app(argc, argv);
QDeclarativeView view;
MyModel model(view.rootContext());
view.setSource(QUrl("qrc:view.qml"));
view.show();
return app.exec();
}
#include "main.moc"
// view.qml
import Qt 4.6
ListView {
width: 200
height: 200
model: VisualDataModel {
model: myModel
rootIndex: myRoot
delegate: Component {
Rectangle {
height: 25; width: 100
Text { text: path }
MouseRegion {
anchors.fill: parent;
onClicked: myModel.setRoot(path)
}
}
}
}
}
| Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies) | Trademarks | Qt 4.7.0 |