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. How to make QFileSystemModel work with a ListView
Forum Updated to NodeBB v4.3 + New Features

How to make QFileSystemModel work with a ListView

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 595 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.
  • G Offline
    G Offline
    GeoffreyDlvl
    wrote on last edited by GeoffreyDlvl
    #1

    Hi, I am unable to make the QFileSystemModel go along with the ListView. All the examples and documentation that I found would use QFileSystemModel with the TreeView (which works fine), but this is not what I need.

    I do not want to use FolderListModel QML type either because I want to add extra roles to my model later and edit/delete files and folders from the application.

    I used DelegateModel to be able to set an other root index. For some reason, my files are correctly displayed for a fraction of a second and then the view jumps to the root of my filesystem. I'm not sure what happens, maybe the indexes are invalidated somehow.

    Below is a sample of my code:

    main.cpp

        QFileSystemModel *fsm = new QFileSystemModel(&engine);
        fsm->setRootPath(QDir::homePath());
        fsm->setResolveSymlinks(true);
        engine.rootContext()->setContextProperty("displayFileSystemModel", fsm);
        engine.rootContext()->setContextProperty("rootPathIndex", fsm->index(fsm->rootPath()));
    

    fsview.qml

    ListView {
        width: 300
        height: 400
    
        model: DelegateModel {
            model: displayFileSystemModel
            rootIndex: rootPathIndex
    
            delegate: Rectangle {
                width: 200; height: 25
                Text {
                    text: model.filePath
                    Component.onCompleted: {
                        console.log(text)
                    }
                }
            }
        }
    }
    

    What am I doing wrong?

    UPDATE

    So, apparently QFileSystemModel uses a separate thread to populate itself and shoots a directoryLoaded signal once the the thread has finished to load the path.

    I tried connecting my view to only set the root index once the path has been properly loaded by adding the following code to my listview

        Connections {
            target: displayFileSystemModel
            function onDirectoryLoaded(path) { delegatemodel.rootIndex = rootPathIndex }
        }
    

    However, this does not solve the problem. A dirty workaround was to set a timer to try set the rootIndex after a period of time.

        Timer {
            interval: 100
            running: true
            onTriggered: delegatemodel.rootIndex = examPathIndex
        }
    

    And this "solves" the problem but of course is far from a satisfactory solution. Any idea?

    UPDATE 2

    Turns out every time the model is updated (and the directoryLoaded signal is triggered), the view resets. Even trying to reassign the rootIndex every time new data is loaded doesn't work.

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by fcarney
      #2

      I think the context property "rootPathIndex" is doing the equivalent of storing an index (QModelIndex). You should not store indexes they can go bad. Instead create a getter or property on your QFileSystemModel (create subclass) to get the root path index on the fly (recreated) each time your delegate is reloaded.

      C++ is a perfectly valid school of magic.

      1 Reply Last reply
      0

      • Login

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