source: trunk/doc/src/examples/qml-folderlistmodel.qdoc

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 5.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:FDL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in a
14** written agreement between you and Nokia.
15**
16** GNU Free Documentation License
17** Alternatively, this file may be used under the terms of the GNU Free
18** Documentation License version 1.3 as published by the Free Software
19** Foundation and appearing in the file included in the packaging of this
20** file.
21**
22** If you have questions regarding the use of this file, please contact
23** Nokia at qt-info@nokia.com.
24** $QT_END_LICENSE$
25**
26****************************************************************************/
27
28/*!
29
30\title FolderListModel - a C++ model plugin
31\example src/imports/folderlistmodel
32
33This plugin shows how to make a C++ model available to QML. It presents
34a simple file list for a single folder (directory) and allows the presented
35folder to be changed.
36
37\image declarative-folderlistmodel.png The FolderListModel used to choose a QML file
38
39We do not explain the model implementation in detail, but rather focus on the mechanics of
40making the model available to QML.
41
42\section1 Usage from QML
43
44The type we are creating can be used from QML like this:
45
46\snippet doc/src/snippets/declarative/folderlistmodel.qml 0
47
48\section1 Defining the Model
49
50We are subclassing QAbstractListModel which will allow us to give data to QML and
51send notifications when the data changes:
52
53\snippet src/imports/folderlistmodel/qdeclarativefolderlistmodel.h class begin
54
55As you see, we also inherit the QDeclarativeParserStatus interface, so that we
56can delay initial processing until we have all properties set (via componentComplete() below).
57
58The first thing to do when devising a new type for QML is to define the properties
59you want the type to have:
60
61\snippet src/imports/folderlistmodel/qdeclarativefolderlistmodel.h class props
62
63The purposes of each of these should be pretty obvious - in QML we will set the folder
64to display (a file: URL), and the kinds of files we want to show in the view of the model.
65
66Next are the constructor, destructor, and standard QAbstractListModel subclassing requirements:
67
68\snippet src/imports/folderlistmodel/qdeclarativefolderlistmodel.h abslistmodel
69
70The data() function is where we provide model values. The rowCount() function
71is also a standard part of the QAbstractListModel interface, but we also want to provide
72a simpler count property:
73
74\snippet src/imports/folderlistmodel/qdeclarativefolderlistmodel.h count
75
76Then we have the functions for the remaining properties which we defined above:
77
78\snippet src/imports/folderlistmodel/qdeclarativefolderlistmodel.h prop funcs
79
80Imperative actions upon the model are made available to QML via a Q_INVOKABLE tag on
81a normal member function. The isFolder(index) function says whether the value at \e index
82is a folder:
83
84\snippet src/imports/folderlistmodel/qdeclarativefolderlistmodel.h isfolder
85
86Then we have the QDeclarativeParserStatus interface:
87
88\snippet src/imports/folderlistmodel/qdeclarativefolderlistmodel.h parserstatus
89
90Then the NOTIFY function for the folders property. The implementation will emit this
91when the folder property is changed.
92
93\snippet src/imports/folderlistmodel/qdeclarativefolderlistmodel.h notifier
94
95The class ends with some implementation details:
96
97\snippet src/imports/folderlistmodel/qdeclarativefolderlistmodel.h class end
98
99Lastly, the boilerplare to declare the type for QML use:
100
101\snippet src/imports/folderlistmodel/qdeclarativefolderlistmodel.h qml decl
102
103\section1 Connecting the Model to QML
104
105To make this class available to QML, we only need to make a simple subclass of QDeclarativeExtensionPlugin:
106
107\snippet src/imports/folderlistmodel/plugin.cpp class decl
108
109and then use the standard Qt plugin export macro:
110
111\snippet src/imports/folderlistmodel/plugin.cpp plugin export decl
112
113Finally, in order for QML to connect the "import" statement to our plugin, we list it in the qmldir file:
114
115\l{src/imports/folderlistmodel/qmldir}
116
117This qmldir file and the compiled plugin will be installed in \c $QTDIR/imports/Qt/labs/folderlistmodel/ where
118the QML engine will find it (since \c $QTDIR/imports is the value of QLibraryInf::libraryPath()).
119
120\section1 Implementing the Model
121
122We'll not discuss the model implementation in detail, as it is not specific to QML - any Qt C++ model
123can be interfaced to QML.
124This implementation is basically just takes the krufty old QDirModel,
125which is a tree with lots of detailed roles and re-presents it as a simpler list model where
126each item is just a fileName and a filePath (as a file: URL rather than a plain file, since QML
127works with URLs for all content).
128
129\l{src/imports/folderlistmodel/qdeclarativefolderlistmodel.cpp}
130*/
Note: See TracBrowser for help on using the repository browser.