Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Show content of specific subfolders using QFilesystemmodel
Forum Updated to NodeBB v4.3 + New Features

Show content of specific subfolders using QFilesystemmodel

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 992 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.
  • M Offline
    M Offline
    Meta
    wrote on last edited by
    #1

    I'm trying to fill a ListView with items from a QFilesystemmodel. But I want to show only specific filetypes from specific subfolders.
    Example: I have a folder structure like that:

    Images
    |-2016
    |  |-events
    |  |-misc
    |-2017
    |  |-events
    |  |-misc
    |-2018
    |  |-events
    |  |-misc
    

    I set the root for the QFilesystemmodel to "Images" and want to display all the contents of every "events" subfolder as a flat list.
    I can filter for the filetypes but I have no clue how to show only the contents of some subfolders. I tried subclassing and modifying the QFilesystemmodel class but couldnt get the needed results. I had a look on QSortfilterproxymodel but dont know if I can get the results with that.
    Does anyone have an idea how to solve that?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      I think you're one the right track. IIRC to flatten the model, you have to re-implement the parent method to return the top level index as parent for all the items you want to show.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        To flatten the model you can use KDescendantsProxyModel written by one of the stars of Qt Model/Views, Stephen Kelly.

        A model that removes the higher hierarchical levels is not currently available but you are not the only one that requested the feature and it's currently on my to-do list for Qt 5.13

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        1
        • M Offline
          M Offline
          Meta
          wrote on last edited by
          #4

          Thanks for the suggestions so far. I tried to flatten the model by modifying the parent method. I added the following parent method to my model class:

          QModelIndex MyModel::parent(const QModelIndex &index) const
          {
              QModelIndex grandParent = QFileSystemModel::parent(index);
              return QFileSystemModel::parent(grandParent);
          }
          

          In my opinion this should "lift up" the model by one level. But now my list is empty. Without the parent method my list shows the top level of folders (2016, 2017, 2018 from the example).

          The description of the KDescendantsProxyModel looks like it does what I need. It seems like a more complex solution and I have currently no idea how I could integrate the class into my project as it has some dependencies with the rest of the KDE framework.

          VRoninV 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            IIRC, you only need the ECM module to build the kitemmodels module.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            1
            • M Meta

              Thanks for the suggestions so far. I tried to flatten the model by modifying the parent method. I added the following parent method to my model class:

              QModelIndex MyModel::parent(const QModelIndex &index) const
              {
                  QModelIndex grandParent = QFileSystemModel::parent(index);
                  return QFileSystemModel::parent(grandParent);
              }
              

              In my opinion this should "lift up" the model by one level. But now my list is empty. Without the parent method my list shows the top level of folders (2016, 2017, 2018 from the example).

              The description of the KDescendantsProxyModel looks like it does what I need. It seems like a more complex solution and I have currently no idea how I could integrate the class into my project as it has some dependencies with the rest of the KDE framework.

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @Meta said in Show content of specific subfolders using QFilesystemmodel:

              It seems like a more complex solution and I have currently no idea how I could integrate the class into my project as it has some dependencies with the rest of the KDE framework.

              See https://forum.qt.io/topic/76533/copy-selected-rows-of-a-table-to-a-model/12

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              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