0

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 ?

boudafc
  • 1
  • 1
  • Why not use http://doc.qt.io/qt-5/qml-qt-labs-folderlistmodel-folderlistmodel.html? – dtech Feb 07 '19 at 13:24
  • @dtech: I don't want to use FolderListModel because I want to add others roles on items (comments etc...). Moreover, I can't access folder outside my project folder... – boudafc Feb 07 '19 at 13:39
  • So you want to add roles without existing underlying data? If you are to have optional comments for files or folders, those can easily be facilitated externally, without inviting complexity in the model. As for the last part - FolderListModel can access your entire available storage, there is no limitation. – dtech Feb 07 '19 at 13:42
  • @dtech: yes, for example, in my first listview I want to display a list of folder. In the second view, display a list of videos filenames. When I click on the filename I can display informations about it but also extra-informations like a comment, duration... I prefer seperate model from view for adding extra future functionnalities, for example exclude some files when filename contain some code ... – boudafc Feb 07 '19 at 15:05
  • I edit my first post with precision – boudafc Feb 08 '19 at 10:35

0 Answers0