Change QSortFilterProxyModel behaviour for multiple column filtering

前端 未结 3 1496
遥遥无期
遥遥无期 2021-01-03 00:55

We have a QSortFilterProxyModel installed on a QTableView and two (or more) QLineEdit for filtering the view (based on the text of the

3条回答
  •  攒了一身酷
    2021-01-03 01:42

    If you want to connect the 2 inputs with a "and" filter you can simply layer them.

    Something like this should work.

    QSortFilterProxyModel namefilter;
    nameFilter.setFilterKeyColumn(nameColum);
    QSortFilterProxyModel yearFilter;
    yearFilter.setFilterKeyColumn(yearColumn);
    
    yearFilter.setSourceModel(model);
    nameFilter.setSourceModel(&yearFilter);
    view.setSource(&nameFilter);
    
    //....
    
    
    void onTextChange(int index, QString ntext)
    {
        switch(index)
        {
            case yearColumn:
                yearFilter.setFilterRegExp(QRegExp(ntext, Qt::CaseInsensitive));
                break;
            case nameColum:
                namefilter.setFilterRegExp(QRegExp(ntext, Qt::CaseInsensitive));
                break;    
        }
    }
    

提交回复
热议问题