Important: Please read the Qt Code of Conduct -

QSortFilterProxyModel is not filtering

  • 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 );

  • 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.

  • @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??

  • @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

Log in to reply