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. QSortFilterProxyModel
Forum Updated to NodeBB v4.3 + New Features

QSortFilterProxyModel

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 3.0k 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.
  • P Offline
    P Offline
    phamtv
    wrote on last edited by
    #1

    I have a Qtreeview displaying a custom QSortFilterProxyModel based on the QFileSystemModel. I need to filter out rows within my root directory. What can I use to filter only files within the rootdirectory that I set (QModelIndex? cast my sourcemodel to QFileSystemModel and compare file name?)? What would be the ideal way to implement this?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      I think that is the only option.
      You could also compare the parent model index with the root model index, but no way around casting the source model.

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        No need for casts, I think. Why not just make the role to apply the filter on a property of the QSFPM?

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          And which property should be used?
          Do you mean: use data() with a specific column? there you don't get the full path, right? So how to check thge parent directory of an element?

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            Sure you have! See the QFileSystemModel::Roles enum.
            Using the right role, you have the full path of every item in the model directly from that item.

            1 Reply Last reply
            0
            • P Offline
              P Offline
              phamtv
              wrote on last edited by
              #6

              Andre, can you provide code snippet? Let's say I have the following directory and I wanted a filter to do the folders below:
              @
              TestSuite <--- Root Directory
              -- common <--- need to filter this folder
              -- tools <--- need to filter this folder
              -- test1 <--- keep this folder
              -- test2 <--- keep this folder
              L cfg <--- need to filter this subfolder
              L testa <--- keep this subfolder
              L testb <--- keep this subfolder
              @

              Basically the logic for my filter will be 1.) Child of root directory and 2) Subdirectory folder must start with test.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                I could, but I won't, sorry. You learn nothing of just copy/pasting snippets from others. The idea is that you will have to do it, so that you learn. What have you tried so far?

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  phamtv
                  wrote on last edited by
                  #8

                  Andre, I was not trying to copy your source by any means. I was hoping your example can help answer some of things I am unsure how to implement efficiently. The following is what I currently have:

                  @
                  bool CheckableProxyModel::filterAcceptsRow ( int source_row, const QModelIndex & source_parent ) const
                  {
                  QModelIndex index0 = sourceModel()->index(source_row, COL_FILE_NAME, source_parent);
                  QString str = ((QFileSystemModel)sourceModel()).fileName(index0);

                  qDebug() << "filterAcceptsRow 0: " << str;
                  
                  if (str == "/")
                      return true;
                  
                  str = ((QFileSystemModel)sourceModel()).filePath(index0);
                  
                  if (str.contains(root))
                  {
                      qDebug() << "filterAcceptsRow 1: " << str;
                  
                      str = str.remove(0, root.length());
                  
                      qDebug() << "filterAcceptsRow 2: " << str;
                  
                      if (str == "" || str.startsWith("test_"))
                          return true;
                  }
                  return false;
                  

                  }
                  @

                  Can you help clarify the following:
                  1.) I am currently using a fixed string for my root directory comparison. Is there a way for me to retrieve it from my source model?
                  2.) How do I iterate through the subdirectories for line 21 and filter out the rows that starts with "test_"

                  You help is greatly appreciated!

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    phamtv
                    wrote on last edited by
                    #9

                    Andre, I got things to work like I wanted. However, what is the best way for me to determine my source model root directory? Do I have to set it manually or can I determine it from my source model?

                    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