Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Subclass of QSortFilterProxyModel to filter selected columns

Subclass of QSortFilterProxyModel to filter selected columns

Scheduled Pinned Locked Moved General and Desktop
qsortfilterproxfilterssubclassing
15 Posts 3 Posters 9.0k Views
  • 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.
  • S Offline
    S Offline
    Stravinsky
    wrote on 18 Jun 2015, 04:01 last edited by
    #5

    I used this code, but this sublclass is usefull for filtering columns with different filter on each of them.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Stravinsky
      wrote on 23 Jun 2015, 18:02 last edited by
      #6

      Solution to this problem is very important for me. Can someone help me? Please

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 23 Jun 2015, 21:37 last edited by
        #7

        The example shown in the filtering part of QFilterProxyModel's documentation shows how to filter only the columns 0, 1 and 2. You should be able to make your model from there.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Stravinsky
          wrote on 25 Jun 2015, 18:02 last edited by Stravinsky
          #8

          I have a subclass of QHeaderView where I'm hiding columns. Also I implement in QHeaderView signal which sends to my QSortFilterProxyModel hidden columns (as bool, true if not hidden). Slot in QSortFilterProxyModel received that signal .I have about 10 different tables, each of them with different number of columns.

          SLOT in QSortFilterProxyModel:

              void SortFilterProxyModel::receiveHiddenColumns(bool* ichecked_columns)
              {
                  checked_columns=ichecked_columns;
                  for(int i=0;i<7;i++){
                  qDebug()<<"R"<<ichecked_columns[i];
                  }
              }
          

          This part of code works and filter only 0 and 1 column:

           bool SortFilterProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent) const
              {
                  QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
                  QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent);
          
          
                  return  (sourceModel()->data(index0).toString().contains(filterRegExp())
                         || sourceModel()->data(index1).toString().contains(filterRegExp()));
           // HOW TO RETURN ONLY NOT HIDDEN COLUMNS????
              }
          

          I have no idea how to return in filterAcceptsRow() the appropriate columns depending on the received hidden.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 25 Jun 2015, 20:39 last edited by
            #9

            One solution would be to add a setter to your SortFilterProxyModel that takes a QList<int> that contains the hidden columns, and in filterAccpetsRow, use that list to check what you should do.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Stravinsky
              wrote on 26 Jun 2015, 16:40 last edited by
              #10

              I have a different question. I declared bool * checked_columns=NULL in header file.

              #ifndef SORTFILTERPROXYMODEL_H
              #define SORTFILTERPROXYMODEL_H
              
              #include <QSortFilterProxyModel>
              #include <QDate>
              
              namespace CerSimpleDynamics
              {
              namespace Models
              {
                  class SortFilterProxyModel : public QSortFilterProxyModel
                  {
                      Q_OBJECT
                  public:
                      explicit SortFilterProxyModel(int columns_maximum, QObject *parent = 0);
                      int columns_max;
                      bool * checked_columns=NULL;
              
                 public slots:
                      void receiveHiddenColumns(bool* ichecked_columns);
              
                 protected:
                      bool filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent) const;
              
                  private:
                  };
              }
              

              }

              #endif // SORTFILTERPROXYMODEL_H
              

              Then i received signal in my SLOT receiveHiddenColumns(). Data is received becouse i see it on the debug screen.

              #include "sortfilterproxymodel.h"
              #include <qdebug.h>
              
              namespace CerSimpleDynamics
              {
              namespace Models
              {
                  SortFilterProxyModel::SortFilterProxyModel(int columns_maximum,QObject *parent) : QSortFilterProxyModel(parent)
                  {
                      columns_max=columns_maximum;
                  }
              
                  void SortFilterProxyModel::receiveHiddenColumns(bool* ichecked_columns)
                  {
                      checked_columns=ichecked_columns;
                      qDebug()<<"RHC-adres checked_columns"<<checked_columns;
                      qDebug()<<"RHC-adres ichecked_columns"<<ichecked_columns;
                      for(int i=0;i<columns_max;i++)
                      {
                          qDebug()<<"RHC"<<checked_columns[i];
                      }
                  }
              
              
                  bool SortFilterProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent) const
                  {
                      qDebug()<<"RFAR-adres checked_columns"<<checked_columns<<endl;
              
                      QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
                      QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent);
                      QModelIndex index2 = sourceModel()->index(sourceRow, 2, sourceParent);
                      bool tab[3];
                      tab[0]=sourceModel()->data(index0).toString().contains(filterRegExp());
                      tab[1]=sourceModel()->data(index1).toString().contains(filterRegExp());
                      tab[2]=sourceModel()->data(index2).toString().contains(filterRegExp());
                      bool all;
                      for (int i=0;i<3;i++)
                      {
                         all = all || tab[i];
                      }
              
                      return all;
                  }
              
              }
              

              }

                #include "moc_sortfilterproxymodel.cpp"
              

              I wolud like to display my pointer variable (checked_columns) in fuction filterAccpeptsRow() but the pointer shows NULL adress as during declaration in header file. Why initialization in SLOT doesn't work?

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 26 Jun 2015, 22:30 last edited by
                #11

                What makes you think it doesn't work ? Is it called somewhere ?

                And why are you using an array of boolean ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Stravinsky
                  wrote on 29 Jun 2015, 12:32 last edited by
                  #12

                  I solved this problem, but I have another one now. I received hidden columns as array of boolean.

                      bool SortFilterProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex &sourceParent) const
                      {
                              bool all=false;
                              bool tab[23];
                              std::fill(tab, tab + columns_max, false);
                              QModelIndex hidden_columns[23];
                              int j=0;
                  
                              for(int i=0;i<sourceModel()->columnCount();i++)
                                  {
                                  if(checked_columns[i]==true)
                                       {
                               hidden_columns[j] = sourceModel()->index(sourceRow, i, sourceParent);                                                                                                                                                              tab[j]=sourceModel()->data(hidden_columns[j]).toString().contains(filterRegExp());
                                       all = all || tab[j];
                                       j++ ;
                                       }
                                  }
                          return all;
                      }
                  

                  It work's but function FilterAcceptsRow() is called as many times as rows in proxy model. I tried to perform a calculation in function only when checking the first row, but it doesnt work.

                  Do you know some solution to skip calculation in every call of function?

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 30 Jun 2015, 12:30 last edited by
                    #13

                    Do you mean do your calculation only on row 0 ?

                    And again: why not just use a QList/QVector<int> to store the hidden columns ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      Stravinsky
                      wrote on 2 Jul 2015, 06:48 last edited by
                      #14

                      1.Do you mean that hidden column is 1 and unhidden column is 0?
                      2.What's the difference in implementation between using array of boolean and qlist/qvector?
                      3. Could you tell me advantege of your solution?

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 2 Jul 2015, 20:55 last edited by
                        #15

                        I don't understand your question 1.

                        1. With a QList of hidden column, you don't need to know the size of the array in your model and you don't need to loop through the complete array to know which columns are hidden or not. You just need to do something like
                        if (!hiddenColumnList.contains(index.column())) { 
                        //  do some stuff if column is not hidden
                        }
                        

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0

                        14/15

                        2 Jul 2015, 06:48

                        • Login

                        • Login or register to search.
                        14 out of 15
                        • First post
                          14/15
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved