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
}
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)
Do you know where I am wrong ?
Thank you for your help.
Edit: 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 ?