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.

    main.cpp:

    QFileSystemModel *model = new QFileSystemModel;
    model->setRootPath(QDir::homePath());
    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:
    main.cpp:

        QFileSystemModel *fsm = new DisplayFileSystemModel(&engine);
        fsm->setRootPath(QDir::homePath());
        fsm->setResolveSymlinks(true);
        engine.rootContext()->setContextProperty("fileSystemModel", fsm);
        engine.rootContext()->setContextProperty("rootPathIndex", fsm->index(fsm->rootPath()));
    

    main.qml:

    TreeView {
        model: fileSystemModel
        rootIndex: rootPathIndex
    

    How to get round that issue?

    Many thanks advance!
    Alexander


  • 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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.