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. Recursive filtering on QSortFilterProxy
Forum Updated to NodeBB v4.3 + New Features

Recursive filtering on QSortFilterProxy

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 2 Posters 314 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.
  • W Offline
    W Offline
    wejgomi
    wrote on 28 Mar 2024, 14:26 last edited by
    #1

    Hi,

    I have hierarchial data in my QStandardItemModel model which is applied to a QTreeView. As for the hierarchy , its depth level is 3 :

    first_level_node
    		second_level_node
    			third_level_node
    

    I want to apply QSortFilterProxyModel to it : If the filter does not match first_level_node's text but if it matches second_level_node or third_level_node , also that first_level_node should stay visible.

    How can I achieve that ?

    I made an attempt by calling setRecursiveFilteringEnabled , however that make entire rows invisible.

    QObject::connect(m_filtering_line_edit, &QLineEdit::textChanged, m_proxy_model, [&](const QString& text) 
     {
         if (text.isEmpty()) 
         {
             m_treeview->setModel(m_model);
             m_treeview->collapseAll();
         }
         else
         {
             QRegularExpression filter("^" + QRegularExpression::escape(text), QRegularExpression::CaseInsensitiveOption);
    		 
    
             m_proxy_model->setFilterRegularExpression(filter);
             m_proxy_model->setFilterKeyColumn(0);
             m_proxy_model->setRecursiveFilteringEnabled(true);
    
             m_treeview->setModel(m_proxy_model);
         }
     });
    

    Many thanks

    J 1 Reply Last reply 28 Mar 2024, 14:40
    0
    • W wejgomi
      28 Mar 2024, 14:26

      Hi,

      I have hierarchial data in my QStandardItemModel model which is applied to a QTreeView. As for the hierarchy , its depth level is 3 :

      first_level_node
      		second_level_node
      			third_level_node
      

      I want to apply QSortFilterProxyModel to it : If the filter does not match first_level_node's text but if it matches second_level_node or third_level_node , also that first_level_node should stay visible.

      How can I achieve that ?

      I made an attempt by calling setRecursiveFilteringEnabled , however that make entire rows invisible.

      QObject::connect(m_filtering_line_edit, &QLineEdit::textChanged, m_proxy_model, [&](const QString& text) 
       {
           if (text.isEmpty()) 
           {
               m_treeview->setModel(m_model);
               m_treeview->collapseAll();
           }
           else
           {
               QRegularExpression filter("^" + QRegularExpression::escape(text), QRegularExpression::CaseInsensitiveOption);
      		 
      
               m_proxy_model->setFilterRegularExpression(filter);
               m_proxy_model->setFilterKeyColumn(0);
               m_proxy_model->setRecursiveFilteringEnabled(true);
      
               m_treeview->setModel(m_proxy_model);
           }
       });
      

      Many thanks

      J Offline
      J Offline
      JonB
      wrote on 28 Mar 2024, 14:40 last edited by
      #2

      @wejgomi
      Per the docs

      This property holds whether the filter to be applied recursively on children, and for any matching child, its parents will be visible as well.

      and I have found that works as documented. If a child matches it is made visible and that means its parents are also visible. So what does your "make entire rows invisible." mean?

      W 1 Reply Last reply 28 Mar 2024, 16:12
      1
      • J JonB
        28 Mar 2024, 14:40

        @wejgomi
        Per the docs

        This property holds whether the filter to be applied recursively on children, and for any matching child, its parents will be visible as well.

        and I have found that works as documented. If a child matches it is made visible and that means its parents are also visible. So what does your "make entire rows invisible." mean?

        W Offline
        W Offline
        wejgomi
        wrote on 28 Mar 2024, 16:12 last edited by
        #3

        @JonB Sorry, I should have clarified. In my data model , there is only a single column , therefore I was referring to all model items as "rows". In other words, when I apply the filter with the regexp in the code, even when I enter a valid text , nothing is visible after filter processing . If the model data is :

        aa 
           bb
        

        And when I type b ( QString& text in the lambda expression ) , I am expecting to see :

        aa 
           bb
        

        Therefore I have asked the question that way assuming my expectation is correct.

        1 Reply Last reply
        0
        • W Offline
          W Offline
          wejgomi
          wrote on 29 Mar 2024, 06:25 last edited by
          #4

          I have found the issue. I was mising that -> m_proxy_model->setSourceModel(m_model);

          J 1 Reply Last reply 29 Mar 2024, 08:36
          0
          • W wejgomi
            29 Mar 2024, 06:25

            I have found the issue. I was mising that -> m_proxy_model->setSourceModel(m_model);

            J Offline
            J Offline
            JonB
            wrote on 29 Mar 2024, 08:36 last edited by JonB
            #5

            @wejgomi
            That is a pretty fundamental omission! Then I don't see how you had it working before you added the filter/setRecursiveFilteringEnabled() at all.

            1 Reply Last reply
            0

            1/5

            28 Mar 2024, 14:26

            • Login

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