Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved filterAcceptsColumn work wrong in QSortFilterProxyModel

    General and Desktop
    qt5 qsortfilterprox
    3
    6
    326
    Loading More Posts
    • 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.
    • Z
      zloi_templar last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • VRonin
        VRonin last edited by

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

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        Z 1 Reply Last reply Reply Quote 2
        • Christian Ehrlicher
          Christian Ehrlicher Lifetime Qt Champion last edited by

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

          Qt has to stay free or it will die.

          1 Reply Last reply Reply Quote 2
          • VRonin
            VRonin last edited by

            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();
            }
            

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            Z 1 Reply Last reply Reply Quote 3
            • Z
              zloi_templar @VRonin last edited by zloi_templar

              @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);
                  };
              
              1 Reply Last reply Reply Quote 0
              • VRonin
                VRonin last edited by

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

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                Z 1 Reply Last reply Reply Quote 2
                • Z
                  zloi_templar @VRonin last edited by

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

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post