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. QFileSystemModel and Listview to list directories
Forum Updated to NodeBB v4.3 + New Features

QFileSystemModel and Listview to list directories

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
8 Posts 4 Posters 2.1k Views
  • 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.
  • B Offline
    B Offline
    boudafc
    wrote on last edited by boudafc
    #1

    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.

    JonBJ 1 Reply Last reply
    0
    • B boudafc

      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.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @boudafc
      I don't do QML, so this may be off-base, but where does your QML reference the lDirModel you created at all?

      B 1 Reply Last reply
      0
      • JonBJ JonB

        @boudafc
        I don't do QML, so this may be off-base, but where does your QML reference the lDirModel you created at all?

        B Offline
        B Offline
        boudafc
        wrote on last edited by
        #3

        @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 the lDirModel you created at all?

        whoops, I didn't copy/paste enought. I have edited my previous post.

        JonBJ 1 Reply Last reply
        0
        • B boudafc

          @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 the lDirModel you created at all?

          whoops, I didn't copy/paste enought. I have edited my previous post.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @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.

          B 1 Reply Last reply
          0
          • JonBJ JonB

            @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.

            B Offline
            B Offline
            boudafc
            wrote on last edited by
            #5

            @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)

            1 Reply Last reply
            0
            • B Offline
              B Offline
              boudafc
              wrote on last edited by
              #6

              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 ?

              1 Reply Last reply
              0
              • terma.abaT Offline
                terma.abaT Offline
                terma.aba
                wrote on last edited by
                #7

                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.

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

                  Use a DelegateModel in between the filesystem model and the ListView.

                  Edit: You can specify the root index in the delegate model.

                  C++ is a perfectly valid school of magic.

                  1 Reply Last reply
                  1

                  • Login

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