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 |
|
---|
33 | This plugin shows how to make a C++ model available to QML. It presents
|
---|
34 | a simple file list for a single folder (directory) and allows the presented
|
---|
35 | folder to be changed.
|
---|
36 |
|
---|
37 | \image declarative-folderlistmodel.png The FolderListModel used to choose a QML file
|
---|
38 |
|
---|
39 | We do not explain the model implementation in detail, but rather focus on the mechanics of
|
---|
40 | making the model available to QML.
|
---|
41 |
|
---|
42 | \section1 Usage from QML
|
---|
43 |
|
---|
44 | The 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 |
|
---|
50 | We are subclassing QAbstractListModel which will allow us to give data to QML and
|
---|
51 | send notifications when the data changes:
|
---|
52 |
|
---|
53 | \snippet src/imports/folderlistmodel/qdeclarativefolderlistmodel.h class begin
|
---|
54 |
|
---|
55 | As you see, we also inherit the QDeclarativeParserStatus interface, so that we
|
---|
56 | can delay initial processing until we have all properties set (via componentComplete() below).
|
---|
57 |
|
---|
58 | The first thing to do when devising a new type for QML is to define the properties
|
---|
59 | you want the type to have:
|
---|
60 |
|
---|
61 | \snippet src/imports/folderlistmodel/qdeclarativefolderlistmodel.h class props
|
---|
62 |
|
---|
63 | The purposes of each of these should be pretty obvious - in QML we will set the folder
|
---|
64 | to display (a file: URL), and the kinds of files we want to show in the view of the model.
|
---|
65 |
|
---|
66 | Next are the constructor, destructor, and standard QAbstractListModel subclassing requirements:
|
---|
67 |
|
---|
68 | \snippet src/imports/folderlistmodel/qdeclarativefolderlistmodel.h abslistmodel
|
---|
69 |
|
---|
70 | The data() function is where we provide model values. The rowCount() function
|
---|
71 | is also a standard part of the QAbstractListModel interface, but we also want to provide
|
---|
72 | a simpler count property:
|
---|
73 |
|
---|
74 | \snippet src/imports/folderlistmodel/qdeclarativefolderlistmodel.h count
|
---|
75 |
|
---|
76 | Then we have the functions for the remaining properties which we defined above:
|
---|
77 |
|
---|
78 | \snippet src/imports/folderlistmodel/qdeclarativefolderlistmodel.h prop funcs
|
---|
79 |
|
---|
80 | Imperative actions upon the model are made available to QML via a Q_INVOKABLE tag on
|
---|
81 | a normal member function. The isFolder(index) function says whether the value at \e index
|
---|
82 | is a folder:
|
---|
83 |
|
---|
84 | \snippet src/imports/folderlistmodel/qdeclarativefolderlistmodel.h isfolder
|
---|
85 |
|
---|
86 | Then we have the QDeclarativeParserStatus interface:
|
---|
87 |
|
---|
88 | \snippet src/imports/folderlistmodel/qdeclarativefolderlistmodel.h parserstatus
|
---|
89 |
|
---|
90 | Then the NOTIFY function for the folders property. The implementation will emit this
|
---|
91 | when the folder property is changed.
|
---|
92 |
|
---|
93 | \snippet src/imports/folderlistmodel/qdeclarativefolderlistmodel.h notifier
|
---|
94 |
|
---|
95 | The class ends with some implementation details:
|
---|
96 |
|
---|
97 | \snippet src/imports/folderlistmodel/qdeclarativefolderlistmodel.h class end
|
---|
98 |
|
---|
99 | Lastly, 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 |
|
---|
105 | To 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 |
|
---|
109 | and then use the standard Qt plugin export macro:
|
---|
110 |
|
---|
111 | \snippet src/imports/folderlistmodel/plugin.cpp plugin export decl
|
---|
112 |
|
---|
113 | Finally, 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 |
|
---|
117 | This qmldir file and the compiled plugin will be installed in \c $QTDIR/imports/Qt/labs/folderlistmodel/ where
|
---|
118 | the QML engine will find it (since \c $QTDIR/imports is the value of QLibraryInf::libraryPath()).
|
---|
119 |
|
---|
120 | \section1 Implementing the Model
|
---|
121 |
|
---|
122 | We'll not discuss the model implementation in detail, as it is not specific to QML - any Qt C++ model
|
---|
123 | can be interfaced to QML.
|
---|
124 | This implementation is basically just takes the krufty old QDirModel,
|
---|
125 | which is a tree with lots of detailed roles and re-presents it as a simpler list model where
|
---|
126 | each item is just a fileName and a filePath (as a file: URL rather than a plain file, since QML
|
---|
127 | works with URLs for all content).
|
---|
128 |
|
---|
129 | \l{src/imports/folderlistmodel/qdeclarativefolderlistmodel.cpp}
|
---|
130 | */
|
---|