Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Toutes les fonctions  ·  Vues d'ensemble  · 

QML VisualDataModel Element

The VisualDataModel encapsulates a model and delegate More...

Properties

Methods

Detailed Description

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.

 import QtQuick 1.0

 Rectangle {
     width: 200; height: 100

     VisualDataModel {
         id: visualModel
         model: ListModel {
             ListElement { name: "Apple" }
             ListElement { name: "Orange" }
         }
         delegate: Rectangle {
             height: 25
             width: 100
             Text { text: "Name: " + name}
         }
     }

     ListView {
         anchors.fill: parent
         model: visualModel
     }
 }

Property Documentation

delegate : Component

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.


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.


read-onlyparts : 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:

 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 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:

 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.0

 ListView {
     id: view
     width: 300
     height: 400

     model: VisualDataModel {
         model: dirModel

         delegate: Rectangle {
             width: 200; height: 25
             Text { text: filePath }

             MouseArea {
                 anchors.fill: parent
                 onClicked: {
                     if (model.hasModelChildren)
                         view.model.rootIndex = view.model.modelIndex(index)
                 }
             }
         }
     }
 }

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

QModelIndex VisualDataModel::modelIndex ( 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 a QModelIndex for the specified index. This value can be assigned to rootIndex.

See also rootIndex.


QModelIndex VisualDataModel::parentModelIndex ()

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.


Publicit�

Best Of

Actualit�s les plus lues

Semaine
Mois
Ann�e

Le blog Digia au hasard

Logo

D�ploiement d'applications Qt Commercial sur les tablettes Windows 8

Le blog Digia est l'endroit privil�gi� pour la communication sur l'�dition commerciale de Qt, o� des r�ponses publiques sont apport�es aux questions les plus pos�es au support. Lire l'article.

Communaut�

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la r�daction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

Cette page est une traduction d'une page de la documentation de Qt, �crite par Nokia Corporation and/or its subsidiary(-ies). Les �ventuels probl�mes r�sultant d'une mauvaise traduction ne sont pas imputables � Nokia. Qt 4.7
Copyright © 2012 Developpez LLC. Tous droits r�serv�s Developpez LLC. Aucune reproduction, m�me partielle, ne peut �tre faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'� 3 ans de prison et jusqu'� 300 000 E de dommages et int�r�ts. Cette page est d�pos�e � la SACD.
Vous avez d�nich� une erreur ? Un bug ? Une redirection cass�e ? Ou tout autre probl�me, quel qu'il soit ? Ou bien vous d�sirez participer � ce projet de traduction ? N'h�sitez pas � nous contacter ou par MP !
 
 
 
 
Partenaires

H�bergement Web