FolderListModel - a C++ model pluginThis plugin shows how to make a C++ model available to QML. It presents a simple file list for a single folder (directory) and allows the presented folder to be changed. We do not explain the model implementation in detail, but rather focus on the mechanics of making the model available to QML. Usage from QMLThe FolderListModel can be used from QML like this: This displays a list of all subfolders and QML files in the current folder. The FolderListModel folder property can be set to change the folder that is currently displayed. Defining the ModelWe are subclassing QAbstractListModel which will allow us to give data to QML and send notifications when the data changes: As you see, we also inherit the QDeclarativeParserStatus interface, so that we can delay initial processing until we have all properties set (via componentComplete() below). The first thing to do when devising a new type for QML is to define the properties you want the type to have: The purposes of each of these should be pretty obvious - in QML we will set the folder to display (a file: URL), and the kinds of files we want to show in the view of the model. Next are the constructor, destructor, and standard QAbstractListModel subclassing requirements: The data() function is where we provide model values. The rowCount() function is also a standard part of the QAbstractListModel interface, but we also want to provide a simpler count property: Then we have the functions for the remaining properties which we defined above: Imperative actions upon the model are made available to QML via a Q_INVOKABLE tag on a normal member function. The isFolder(index) function says whether the value at index is a folder: Then we have the QDeclarativeParserStatus interface: Then the NOTIFY function for the folders property. The implementation will emit this when the folder property is changed. The class ends with some implementation details: Lastly, the boilerplare to declare the type for QML use: Connecting the Model to QMLTo make this class available to QML, we only need to make a simple subclass of QDeclarativeExtensionPlugin: class QmlFolderListModelPlugin : public QQmlExtensionPlugin { Q_OBJECT Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface" FILE "folderlistmodel.json") public: virtual void registerTypes(const char *uri) { Q_ASSERT(QLatin1String(uri) == QLatin1String("Qt.labs.folderlistmodel")); #ifndef QT_NO_DIRMODEL qmlRegisterType<QQuickFolderListModel>(uri,1,0,"FolderListModel"); #endif } }; and then use the standard Qt plugin export macro: Finally, in order for QML to connect the "import" statement to our plugin, we list it in the qmldir file: src/imports/folderlistmodel/qmldir This qmldir file and the compiled plugin will be installed in $QTDIR/imports/Qt/labs/folderlistmodel/ where the QML engine will find it (since $QTDIR/imports is the value of QLibraryInf::libraryPath()). Implementing the ModelWe'll not discuss the model implementation in detail, as it is not specific to QML - any Qt C++ model can be interfaced to QML. This implementation is basically just takes the krufty old QDirModel(obsolete), which is a tree with lots of detailed roles and re-presents it as a simpler list model where each item is just a fileName and a filePath (as a file: URL rather than a plain file, since QML works with URLs for all content). src/imports/folderlistmodel/qdeclarativefolderlistmodel.cpp Files:
|