Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

filterAcceptsColumn work wrong in QSortFilterProxyModel



  • Hello, i have some trouble with filter
    I have 25 columns, need to hide some columns by filter

    bool ProxyModelSM::filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const
    {
        if(uncheckedColumns.contains(source_column))
        {
            qDebug()<<sourceModel()->headerData(source_column, Qt::Horizontal);
            return false;
        }
        return true;
    }
    

    where uncheckedColumns - list with hided columns (it's numbers in table)
    But filter always hide last columns
    If i send to filter next list (3,7), it's hide 24,25 columns (2 last).
    qDebug print correct header name, but hide last columns
    What i did wrong?



  • just delete ProxyModelSM::headerData completely. The default implementation already does what you want


  • Lifetime Qt Champion

    @zloi_templar said in filterAcceptsColumn work wrong in QSortFilterProxyModel:

    uncheckedColumns

    Are you sure you container contains the source columns and not the proxy columns? I would guess not. Please show use how you fill this container.



  • Can't reproduce.

    Could you test this minimal example?

    #include <QApplication>
    #include <QStandardItemModel>
    #include <QSortFilterProxyModel>
    #include <QTableView>
    
    class CustomProxy : public QSortFilterProxyModel{
        Q_DISABLE_COPY(CustomProxy)
    public:
        using QSortFilterProxyModel::QSortFilterProxyModel;
        const QVector<int>& uncheckedColumns() const{return m_uncheckedColumns;}
        void setUncheckedColumns(const QVector<int> &uncheckedColumns){m_uncheckedColumns = uncheckedColumns;}
    protected:
        bool filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const override{
            if(source_parent.isValid())
                return true;
            return !m_uncheckedColumns.contains(source_column);
        }
    private:
        QVector<int> m_uncheckedColumns;
    };
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc,argv);
        QTableView view;
        QAbstractItemModel* srcModel = new QStandardItemModel(&view);
        srcModel->insertRows(0,100);
        srcModel->insertColumns(0,25);
        for(int i=0;i<srcModel->rowCount();++i){
            for(int j=0;j<srcModel->columnCount();++j){
                srcModel->setData(srcModel->index(i,j),QStringLiteral("%1,%2").arg(i).arg(j));
            }
        }
        CustomProxy* proxyModel = new CustomProxy(&view);
        proxyModel->setSourceModel(srcModel);
        proxyModel->setUncheckedColumns({3,7});
        view.setModel(proxyModel);
        view.show();
        return app.exec();
    }
    


  • @VRonin, @Christian-Ehrlicher sorry, my bad.
    I tested again and saw, that programm works, it's hide correct data column, but draw old header, because i write it manually:

    tbSC = new tableSM(this);
    proxySM = new ProxyModelSM(tbSC);
    mdlSC = new modelSM(proxySM);
    
    QVariant modelSM::headerData(int section, Qt::Orientation orientation, int role) const
    {
        if(role!= Qt::DisplayRole)
            return QVariant();
    
        if (orientation == Qt::Horizontal)
        {
            switch (section)
            {
                case 0:
                    return tr("ID");
                case 1:
                    return tr("Date");
                case 2:
                    return tr("Status");
                case 3:
                    return tr("Street");
                case 4:
                    return tr("Home");
                case 5:
                    return tr("Room");
                case 6:
                    return tr("People");
                case 7:
                    return tr("Animals");
    etc
    
    QVariant ProxyModelSM::headerData(int section, Qt::Orientation orientation, int role) const
     {
           return sourceModel()->headerData(section, orientation, role);
     };
    

    how can i fix it?

    P.S. Fix it

    QVariant ProxyModelSM::headerData(int section, Qt::Orientation orientation, int role) const
        {
            if(orientation==Qt::Horizontal)
                return sourceModel()->headerData(mapToSource(index(0, section)).column(), orientation, role);
            else
                return sourceModel()->headerData(section, orientation, role);
        };
    


  • just delete ProxyModelSM::headerData completely. The default implementation already does what you want



  • @VRonin
    wow, it's really work. Thank you


Log in to reply