Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QFileSystemModel and QML TableView shows root
Qt 6.11 is out! See what's new in the release blog

QFileSystemModel and QML TableView shows root

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 2 Posters 2.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maidmantis
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @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.

      157

      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved