Important: Please read the 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
            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:


    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{
        using QSortFilterProxyModel::QSortFilterProxyModel;
        const QVector<int>& uncheckedColumns() const{return m_uncheckedColumns;}
        void setUncheckedColumns(const QVector<int> &uncheckedColumns){m_uncheckedColumns = uncheckedColumns;}
        bool filterAcceptsColumn(int source_column, const QModelIndex &source_parent) const override{
                return true;
            return !m_uncheckedColumns.contains(source_column);
        QVector<int> m_uncheckedColumns;
    int main(int argc, char *argv[])
        QApplication app(argc,argv);
        QTableView view;
        QAbstractItemModel* srcModel = new QStandardItemModel(&view);
        for(int i=0;i<srcModel->rowCount();++i){
            for(int j=0;j<srcModel->columnCount();++j){
        CustomProxy* proxyModel = new CustomProxy(&view);
        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");
    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
                return sourceModel()->headerData(mapToSource(index(0, section)).column(), orientation, role);
                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