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.qmlTableView { model: dirModel TableViewColumn { role: "fileName" title: "Name" } TableViewColumn { role: "size" title: "Size" } }mypathshows/home/myuserpath. It's expected. ButTableViewshows root of file system (it's just/on Linux).
Could somebody help and explain how to makeTableViewshow the correct folder?P.S. There is example of QML TreeView. It has
rootIndexwhich 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: rootPathIndexHow to get round that issue?
Many thanks advance!
Alexander -
@maidmantis
setRootIndexor in case of QMLTreeViewrootPathIndexis responsible to do make it work which is not provided inTableViewso it wont work in latter case.To explore the FileSystem it is best to use
TreeViewso that the hierarchical file system can be displayed.