[Interest] How to filter QFileSystemModel for use in QML

Julius Bullinger Julius.Bullinger at asctec.de
Thu Aug 13 14:14:21 CEST 2015


Dear list,

I'm having problems using QFileSystemModel in QML.

Take the following example code:

    // QQmlEngine* engine
    QFileSystemModel* fsm = new QFileSystemModel(engine);
    QModelIndex root = fsm->setRootPath("C:/Qt");
    engine->rootContext()->setContextProperty("fileSystemModel", fsm);

Now, using for example a QTreeView, one could set the root by using 
``QTreeWidget::setRootIndex(root)`` to filter everything above the root
level. But suppose I'd like to use the model in QML, e.g. in a TreeView
QML component which doesn't have a setRoot() property.

I tried using a QSortFilterProxyModel like so:

    QSortFilterProxyModel * proxy = new QSortFilterProxyModel ();
    proxy->setSourceModel(fsm);
    proxy->setFilterRole(QFileSystemModel::FilePathRole);
    proxy->setFilterFixedString("C:/Qt");

    engine->rootContext()->setContextProperty("imageListModel", proxy);

which results in an empty list. I remember reading that elements get
filtered out if their parents get filtered out, which means in this
case that "C:/Qt" (and children) is filtered because its parent "C:/"
doesn't fit the filter.

What is the intended way filter QFileSystemModel in this case? I
couldn't find any documentation or examples even after extensive 
google-ing.

Any hints would be really appreciated!

Best regards,
Julius



More information about the Interest mailing list