⚠️ Forum Maintenance: Feb 6th, 8am - 14pm (UTC+2)

QFileSystemModel and QML TableView shows root

  • Hi guys,

    I'm learning QtQuick and QML. And try to explore filesystem on PC. I used FolderListModel from Qt.labs.folderlistmodel and TableView. It works fine. But

    Note: This type is made available by importing the Qt.labs.folderlistmodel module. Elements in the Qt.labs module are not guaranteed to remain compatible in future versions.

    Then I have tried to use QFileSystemModel.


    QFileSystemModel *model = new QFileSystemModel;
    engine.rootContext()->setContextProperty("mypath", QDir::homePath());
    engine.rootContext()->setContextProperty("dirModel", model);

    And main.qml

    TableView {
      model: dirModel
      TableViewColumn {
        role: "fileName"
        title: "Name"
      TableViewColumn {
        role: "size"
        title: "Size"

    mypath shows /home/myuser path. It's expected. But TableView shows root of file system (it's just / on Linux).
    Could somebody help and explain how to make TableView show the correct folder?

    P.S. There is example of QML TreeView. It has rootIndex which allow to set listed folder:

        QFileSystemModel *fsm = new DisplayFileSystemModel(&engine);
        engine.rootContext()->setContextProperty("fileSystemModel", fsm);
        engine.rootContext()->setContextProperty("rootPathIndex", fsm->index(fsm->rootPath()));


    TreeView {
        model: fileSystemModel
        rootIndex: rootPathIndex

    How to get round that issue?

    Many thanks advance!

  • Moderators

    @maidmantis setRootIndex or in case of QML TreeView rootPathIndex is responsible to do make it work which is not provided in TableView so it wont work in latter case.

    To explore the FileSystem it is best to use TreeView so that the hierarchical file system can be displayed.

Log in to reply