From 485d039e7b8ddb150b0f9723cfb08815eb843cba Mon Sep 17 00:00:00 2001 From: Gabriel de Dietrich Date: Sun, 2 Aug 2015 18:45:42 +0200 Subject: filesystembrowser: Improve example by exposing model roles This is one of the biggest API shortcomings we currently have when exposing C++ models to QML. The least we can do is make our examples more readable, and hint the users how to make their apps a bit better. Change-Id: I25f51ef393bbd0c9bf7bab48b705a672881d80c1 Reviewed-by: Friedemann Kleint Reviewed-by: Mitch Curtis --- examples/quick/controls/filesystembrowser/main.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'examples/quick/controls/filesystembrowser/main.cpp') diff --git a/examples/quick/controls/filesystembrowser/main.cpp b/examples/quick/controls/filesystembrowser/main.cpp index 1322bc497..ac24bada8 100644 --- a/examples/quick/controls/filesystembrowser/main.cpp +++ b/examples/quick/controls/filesystembrowser/main.cpp @@ -88,16 +88,18 @@ static inline QString sizeString(const QFileInfo &fi) } class DisplayFileSystemModel : public QFileSystemModel { + Q_OBJECT public: explicit DisplayFileSystemModel(QObject *parent = Q_NULLPTR) : QFileSystemModel(parent) {} - enum { + enum Roles { SizeRole = Qt::UserRole + 4, DisplayableFilePermissionsRole = Qt::UserRole + 5, LastModifiedRole = Qt::UserRole + 6, - UrlStringRole = Qt::UserRole + 7 // 263 + UrlStringRole = Qt::UserRole + 7 }; + Q_ENUM(Roles) QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE { @@ -133,6 +135,8 @@ int main(int argc, char *argv[]) QApplication app(argc, argv); QQmlApplicationEngine engine; + qmlRegisterUncreatableType("io.qt.examples.quick.controls.filesystembrowser", 1, 0, + "FileSystemModel", "Cannot create a FileSystemModel instance."); QFileSystemModel *fsm = new DisplayFileSystemModel(&engine); fsm->setRootPath(QDir::homePath()); fsm->setResolveSymlinks(true); @@ -142,3 +146,5 @@ int main(int argc, char *argv[]) return app.exec(); } + +#include "main.moc" -- cgit v1.2.3