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. QFileSystemModel and QSortFilterProxyModel - root index is not preserved

QFileSystemModel and QSortFilterProxyModel - root index is not preserved

Scheduled Pinned Locked Moved Unsolved General and Desktop
c++qfilesystemmodeqsortfilterproxfiltering
25 Posts 8 Posters 6.7k 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.
  • M Offline
    M Offline
    MasterBLB
    wrote on 26 Jun 2019, 07:29 last edited by
    #1

    Mates,

    I've encountered a strange issue - if I try to filter out something using QSortFilterProxyModel::setFilterFixedString() then root index set for the model is abandoned, and view shows whole path starting from drive letter. What might be the cause of such behavior?
    Qt version: Qt 5.11.1
    compiler: Mingw 5.3.0

    Code:

    //Legend:
    //navigationModel -> class NavigationModel : public QFileSystemModel
    //filteringModel -> class FilteringModel : public QSortFilterProxyModel
    
        //ustaw główny model do wyboru paczki do zainstalowania
        QString root(qApp->applicationDirPath() + "/TreeInGUI");
        navigationModel.setRootPath(root);
    
        //ustaw styl klasycznego windowsa dla drzewka nawigacyjnego aby pokazywało ładne prowadnice
        ui->treeView->setStyle(QStyleFactory::create("windows"));
    
        //filtry trybu admina
        navigationModel.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Files);
        navigationModel.setNameFilterDisables(true);
    
        //model nawigacyjny ma współpracować tylko z plikiem instaluj, oraz skrótami do paczek
        QStringList names{"instaluj", "*.lnk"};
        navigationModel.setNameFilters(names);
    
    //filteringModel na razie nie działa do końca dobrze:/
        filteringModel.setSourceModel(&navigationModel);
        filteringModel.setRecursiveFilteringEnabled(true);
        filteringModel.setFilterCaseSensitivity(Qt::CaseInsensitive);
    
        //ui->treeView->setModel(&navigationModel);
        //ui->treeView->setRootIndex(navigationModel.index(root));
        ui->treeView->setModel(&filteringModel);
        ui->treeView->setRootIndex(filteringModel.mapFromSource(navigationModel.index(root)));
    
        //filteringModel.setFilterFixedString("Testy");
    

    Below how it looks with set root and no active filter (last line from the code commented) - it's OK:
    1_1561533914790_No filtering - ok.PNG

    And this is the result is I uncomment last code line - true, the filtering is performed, however root index is not preserved which is not OK:
    0_1561533914790_Filtering enabled - not ok.PNG

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MasterBLB
      wrote on 28 Jun 2019, 05:53 last edited by
      #2

      Seriously no one ever has experienced such behavior?

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 28 Jun 2019, 06:02 last edited by
        #3

        Hi,

        That might be the case.

        One thing you can do to get more answers: provide a minimal compilable example to reproduce that behaviour. While you are giving some code to reproduce it, people will have to rewrite it in order to build something functional that may or may not trigger the issue you have. If everybody has the same code, then it's going to be way easier to find out whether it's a Qt issue or something else.

        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
        2
        • M Offline
          M Offline
          MasterBLB
          wrote on 28 Jun 2019, 06:24 last edited by
          #4

          It is minimal example. In the comments at the beginning I've written what base classes are used, and implementation details are not important as they are related to supporting drag and drop only, and custom icons.

          J 1 Reply Last reply 28 Jun 2019, 06:30
          0
          • M MasterBLB
            28 Jun 2019, 06:24

            It is minimal example. In the comments at the beginning I've written what base classes are used, and implementation details are not important as they are related to supporting drag and drop only, and custom icons.

            J Online
            J Online
            jsulm
            Lifetime Qt Champion
            wrote on 28 Jun 2019, 06:30 last edited by jsulm
            #5

            @MasterBLB "minimal compilable example".
            What you provided are some code snippets, nothing one can compile.
            It is important to have a minimal example showing the issue, because the issue could be in other parts of the code than what you posted.
            Also, if you only post code snippets you're actually asking others to create a minimal compilable example...

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            2
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 28 Jun 2019, 06:32 last edited by
              #6

              Sorry but no it's not.

              It's neither minimal nor compilable.

              • It can't be just copied and pasted in a main.cpp to be built.
              • There's no main function to launch the application
              • There's a designer based ui used that is not available
              • You are using subclasses that are maybe doing something or not so we don't know whether it's in that part that something is going on.

              Therefore, you are asking people to write and build themselves that minimal use case to find out whether they can reproduce your issue.

              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 Offline
                M Offline
                MasterBLB
                wrote on 28 Jun 2019, 07:10 last edited by
                #7

                Well, I was sure Qt Champions are capable of:

                • creaing a QTreeView instance
                • creating a QFileSystemModel instance
                • creating a QSortFilterProxyModel instance
                • apply a simple filter for the filter model

                well then, here is main:

                int main(int argc, char *argv[])
                {
                   QApplication a(argc, argv);
                    
                   QTreeView treeView;
                
                   QFileSystemModel navigationModel;
                    navigationModel.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Files);
                    navigationModel.setNameFilterDisables(true);
                QString root(/*set it somewhere into directory structure, say C:/Qt/some version*/);
                navigationModel.setRootPath(root);
                
                QSortFilterProxyModel filteringModel;
                filteringModel.setSourceModel(&navigationModel);
                    filteringModel.setRecursiveFilteringEnabled(true);
                    filteringModel.setFilterCaseSensitivity(Qt::CaseInsensitive);
                
                    treeView.setModel(&filteringModel);
                    treeView.setRootIndex(filteringModel.mapFromSource(navigationModel.index(root)));
                
                filteringModel.setFilterFixedString(/*put some folder name which appears deeper in the choosen root*/);
                
                    treeView.show();
                
                   return a.exec();
                }
                
                J 1 Reply Last reply 28 Jun 2019, 09:21
                0
                • M MasterBLB
                  28 Jun 2019, 07:10

                  Well, I was sure Qt Champions are capable of:

                  • creaing a QTreeView instance
                  • creating a QFileSystemModel instance
                  • creating a QSortFilterProxyModel instance
                  • apply a simple filter for the filter model

                  well then, here is main:

                  int main(int argc, char *argv[])
                  {
                     QApplication a(argc, argv);
                      
                     QTreeView treeView;
                  
                     QFileSystemModel navigationModel;
                      navigationModel.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Files);
                      navigationModel.setNameFilterDisables(true);
                  QString root(/*set it somewhere into directory structure, say C:/Qt/some version*/);
                  navigationModel.setRootPath(root);
                  
                  QSortFilterProxyModel filteringModel;
                  filteringModel.setSourceModel(&navigationModel);
                      filteringModel.setRecursiveFilteringEnabled(true);
                      filteringModel.setFilterCaseSensitivity(Qt::CaseInsensitive);
                  
                      treeView.setModel(&filteringModel);
                      treeView.setRootIndex(filteringModel.mapFromSource(navigationModel.index(root)));
                  
                  filteringModel.setFilterFixedString(/*put some folder name which appears deeper in the choosen root*/);
                  
                      treeView.show();
                  
                     return a.exec();
                  }
                  
                  J Online
                  J Online
                  jsulm
                  Lifetime Qt Champion
                  wrote on 28 Jun 2019, 09:21 last edited by jsulm
                  #8

                  @MasterBLB said in QFileSystemModel and QSortFilterProxyModel - root index is not preserved:

                  Well, I was sure Qt Champions are capable of

                  You're the one asking for help. It is not about whether Qt champions are capable of doing this or not, it's simply about to help others to help you. You're aware that your list means work for others, right? People in this forum are not paid workers who have to work for you, but just volunteers spending their own time to help others...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  2
                  • M Offline
                    M Offline
                    MasterBLB
                    wrote on 28 Jun 2019, 09:48 last edited by
                    #9

                    And that's why I've written the example within around 60s, as requested. I hope it fulfills expectations, but if needed feel free to ask about implementation details of models, though as mentioned there isn't anything related to manipulating root index.

                    1 Reply Last reply
                    0
                    • mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 28 Jun 2019, 15:40 last edited by
                      #10

                      Hi
                      I can confirm it get the same result on Qt5.12.3 and Qt5.9
                      so at least we can say its not a local bug of sorts.
                      alt text

                      #include <QApplication>
                      #include <QFileSystemModel>
                      #include <QSortFilterProxyModel>
                      #include <QTreeView>
                      
                      int main(int argc, char *argv[])
                      {
                          QApplication a(argc, argv);
                      
                          QTreeView treeView;
                      
                          QFileSystemModel navigationModel;
                          navigationModel.setFilter(QDir::AllDirs | QDir::NoDotAndDotDot | QDir::Files);
                          navigationModel.setNameFilterDisables(true);
                          QString root("C:/Qt/5.12.3/msvc2017_64");
                          navigationModel.setRootPath(root);
                      
                          QSortFilterProxyModel filteringModel;
                          filteringModel.setSourceModel(&navigationModel);
                          filteringModel.setRecursiveFilteringEnabled(true);
                          filteringModel.setFilterCaseSensitivity(Qt::CaseInsensitive);
                      
                          treeView.setModel(&filteringModel);
                          treeView.setRootIndex(filteringModel.mapFromSource(navigationModel.index(root)));
                      
                          filteringModel.setFilterFixedString("Lib");
                      
                          treeView.show();
                      
                          return a.exec();
                      }
                      
                      
                      1 Reply Last reply
                      3
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 28 Jun 2019, 19:01 last edited by
                        #11

                        As @mrjj I can reproduce that on macOS with a recent build of Qt.

                        You should check the bug report system to see if there's already something related. If not, then please consider opening a new report providing that minimal compilable example.

                        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
                        3
                        • M Offline
                          M Offline
                          MasterBLB
                          wrote on 28 Jun 2019, 20:10 last edited by
                          #12

                          Oh crap, tough luck :/ How to open a Qt bug report?

                          mrjjM 1 Reply Last reply 28 Jun 2019, 20:12
                          0
                          • M MasterBLB
                            28 Jun 2019, 20:10

                            Oh crap, tough luck :/ How to open a Qt bug report?

                            mrjjM Offline
                            mrjjM Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 28 Jun 2019, 20:12 last edited by
                            #13

                            @MasterBLB
                            Hi
                            Well you just go there and login with the credentials you use here.
                            A good read is
                            https://wiki.qt.io/Reporting_Bugs

                            1 Reply Last reply
                            2
                            • M Offline
                              M Offline
                              MasterBLB
                              wrote on 2 Jul 2019, 08:47 last edited by
                              #14

                              @mrjj , @SGaist - have you reported the bug? If not I will try when I'll go back home after work.

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on 2 Jul 2019, 08:51 last edited by
                                #15

                                Personally, I didn't. Since you found it, the honour is yours :)

                                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
                                • mrjjM Offline
                                  mrjjM Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 2 Jul 2019, 09:03 last edited by
                                  #16

                                  Hi
                                  nope, if i had made report, i would have linked url here :)
                                  So please do.

                                  1 Reply Last reply
                                  0
                                  • VRoninV Offline
                                    VRoninV Offline
                                    VRonin
                                    wrote on 2 Jul 2019, 09:15 last edited by
                                    #17

                                    I don't think it's a bug.
                                    setRootIndex is a method of the view. The model has no way of knowing (and shouldn't know) what a view is using as root. The filter set on the proxy applies to the whole model. If the index used as root by a view gets filtered out it's only natural for a view to revert to using QModelIndex() as root.

                                    What you need is a proxy model that implements the setRootIndex functionality at the model level. This is on the list of nice-things-I-would-like-to-add-to-Qt-models and it's a fairly easy one to do but I need to find time for it :(

                                    "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
                                    5
                                    • M Offline
                                      M Offline
                                      MasterBLB
                                      wrote on 2 Jul 2019, 18:34 last edited by
                                      #18

                                      ...and now I'm confused :/

                                      JonBJ 1 Reply Last reply 2 Jul 2019, 18:41
                                      0
                                      • M MasterBLB
                                        2 Jul 2019, 18:34

                                        ...and now I'm confused :/

                                        JonBJ Offline
                                        JonBJ Offline
                                        JonB
                                        wrote on 2 Jul 2019, 18:41 last edited by
                                        #19

                                        @MasterBLB
                                        Confused in what way? I have just read this thread and what @VRonin has written seems correct to me. Are you not understanding what he is saying, or what?

                                        1 Reply Last reply
                                        0
                                        • M Offline
                                          M Offline
                                          MasterBLB
                                          wrote on 3 Jul 2019, 07:27 last edited by
                                          #20

                                          In the way such behavior breaks SOLID substitute rule - I can't use descendant class QFileSystemModel like it was its base class QAbstractItemModel in QSortFilterProxyModel. While not a 100% bug it's definitely a serious design flaw, and worth to be reported. But where?

                                          J VRoninV 2 Replies Last reply 3 Jul 2019, 08:01
                                          0

                                          1/25

                                          26 Jun 2019, 07:29

                                          • Login

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