QFileSystemModel and Listview to list directories
-
Hello,
I have a problem using QFileSystemModel and Listview (qml).
I want to use 2 listviews:- one to list directories of a directory
- one to list files available in the directory selected in the first view
At first, I am trying to list directories in a listview however, it seems the rootIndex is not well set because only "/" item is displayed:
Below my sample code:
main.cpp:
QFileSystemModel *lDirModel = new QFileSystemModel(); QDir lDir = QDir("/home/toto"); lDirModel->setRootPath("/home/toto"); lDirModel->setFilter(QDir::AllEntries | QDir::AllDirs); qml_engine->rootContext()->setContextProperty("mediaModel", lDirModel); qml_engine->rootContext()->setContextProperty("mediaRootModel", lDirModel->index("/home/toto"));
media.qml
DelegateModel { id: visualModel model: mediaModel rootIndex: mediaRootModel delegate: Rectangle { color: "red" height: 40 width: 500 Text { text: "Name: " + filePath} } } ListView { anchors.fill: parent clip: true model: visualModel }
Do you know where I am wrong ?
Thank you for your help.
-
@JonB said in QFileSystemModel and Listview to list directories:
@boudafc
I don't do QML, so this may be off-base, but where does your QML reference thelDirModel
you created at all?whoops, I didn't copy/paste enought. I have edited my previous post.
-
@JonB said in QFileSystemModel and Listview to list directories:
@boudafc
I don't know why yours/QML is not working, but BTW complete code for what you want with your 2 panes is in https://www.bogotobogo.com/Qt/Qt5_QTreeView_QFileSystemModel_ModelView_MVC.php.Hello JonB, thank you for your example. Indeed, I have already tested it with QListView & QTreeView and it works well.
I have tested it also with TreeView (qml) it "works" even if rootIndex is not taken into account (it starts from "/")The problem seems to be on the rootIndex definition in ListView. I don't know where I am wrong. Normally, it has to work .
I have read http://doc.qt.io/qt-5/qtquick-modelviewsdata-cppmodels.html but the example is for QAbstractListModel not QAbstractItemModel (QFileSystemModel inherits)
-
The strange things is that using qlistview, it works and display $HOME content:
QFileSystemModel *lModel = new QFileSystemModel(); QModelIndex lIndex = lModel->setRootPath("/home/toto"); _ui->recordsListView->setModel(lModel); _ui->recordsListView->setRootIndex(lIndex);
Somebody can help ?
-
This is not great. It looks like QFileSystemModel will only work on a view which starts with a specific rootIndex. So that won't work with ListView. Looks like it has specifically been written for QTreeView, as in all the examples.
If you look at Qt Labs folderlistmodel, it doesn't use QFileSystemModel, it uses its own FileInfoThread
I've tried all kinds of tricks to derive from QFileSystemModel and trick it via parent(), index(), data() etc. into using a given root index, no luck.
-
Use a DelegateModel in between the filesystem model and the ListView.
Edit: You can specify the root index in the delegate model.