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

QSortFilterProxyModel is not filtering

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 872 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.
  • T Offline
    T Offline
    ThomasKK
    wrote on last edited by
    #1

    I have a very simple use case. I have subclassed a QListView to only show items from a model that have "table" written in the fourth column. So I subclass setModel to catch the sourcemodel and show the proxymodel instead. But the model still shows all items and it doesn't filter.

    What could be the error here?

    void OListView::setModel(QAbstractItemModel *model)
    {
        m_proxymodel->setSourceModel( model );
        m_proxymodel->setFilterRegExp( QRegExp("table", Qt::CaseInsensitive,
                                           QRegExp::FixedString) );
        m_proxymodel->setFilterKeyColumn( 4 );
        QListView::setModel( m_proxymodel );
    }
    
    JonBJ 1 Reply Last reply
    0
    • artwawA Offline
      artwawA Offline
      artwaw
      wrote on last edited by
      #2

      Hi,
      I am not sure if your approach is sound.
      Particularly you don't need to subclass QListView at all.
      Secondly, QRegExp is deprecated, you should use QRegularExpression (there is a separate setter method, setFilterRegularExpression
      So, the approach would be (apart from using QRegularExpression):

      • have a table model, let's say QSqlTableModel;
      • then have a filtermodel - QSortFilterProxyModel;
      • set source model in the filtermodel and assign filtermodel itself as a model to QListView with setModel() method.

      It is all described in the documentation.

      Having said that - after implementing the approach given above it all should work. You should also not forget to set filterRole property if the role you use is different than Qt::DisplayRole.

      Please try and let us know if this sorted your problem out or if you need more help.

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      0
      • T ThomasKK

        I have a very simple use case. I have subclassed a QListView to only show items from a model that have "table" written in the fourth column. So I subclass setModel to catch the sourcemodel and show the proxymodel instead. But the model still shows all items and it doesn't filter.

        What could be the error here?

        void OListView::setModel(QAbstractItemModel *model)
        {
            m_proxymodel->setSourceModel( model );
            m_proxymodel->setFilterRegExp( QRegExp("table", Qt::CaseInsensitive,
                                               QRegExp::FixedString) );
            m_proxymodel->setFilterKeyColumn( 4 );
            QListView::setModel( m_proxymodel );
        }
        
        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @ThomasKK
        In addition to what @artwaw has written.

        have "table" written in the fourth column.

        Why setFilterReg...() anything when there is QSortFilterProxyModel::setFilterFixedString(), does that do a fixed string without any regular expressions?

        What do you mean by "fourth"? How does that compare to your setFilterKeyColumn( 4 )? Fifth??

        T 1 Reply Last reply
        1
        • JonBJ JonB

          @ThomasKK
          In addition to what @artwaw has written.

          have "table" written in the fourth column.

          Why setFilterReg...() anything when there is QSortFilterProxyModel::setFilterFixedString(), does that do a fixed string without any regular expressions?

          What do you mean by "fourth"? How does that compare to your setFilterKeyColumn( 4 )? Fifth??

          T Offline
          T Offline
          ThomasKK
          wrote on last edited by
          #4

          @JonB I have also tried with setFilterFixedString and the proxymodel still doesn't filter. I don't need to set filterRole as it is display role be default.
          As I understand the docs, I shouldn't need to subclass the proxymodel for such a simple use case. It should be working out of the box.
          You are right I misspelled. It should be fifth column but the code is correct.

          Let me share a screen shot of the table I'm trying to filter.!
          Screenshot 2020-09-21 at 15.37.41.png

          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