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. How to I use filterAcceptsRow() function ?
Forum Updated to NodeBB v4.3 + New Features

How to I use filterAcceptsRow() function ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 4.6k 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.
  • T Offline
    T Offline
    tako
    wrote on last edited by
    #1

    Don't give me url. Only view sample code with filterAcceptsRow().

    1 Reply Last reply
    0
    • B Offline
      B Offline
      bsomervi
      wrote on last edited by
      #2

      http://doc.qt.io/qt-5/qsortfilterproxymodel.html#details

      It is a URL but if you read it under the sub heading "Filtering" you will see a code example.

      1 Reply Last reply
      0
      • T Offline
        T Offline
        tako
        wrote on last edited by
        #3

        I just want to appear in the folder I want. (QTreeview) How can I do?

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mjsurette
          wrote on last edited by
          #4

          Here is a sample from something I'm working on. It accepts only rows where the string _search is contained in one of the specified columns.

          sample.h

          class StatusFilter : public QSortFilterProxyModel
          {
          public:
              explicit StatusFilter(QObject * parent = 0);
              ~StatusFilter()= default;
          
              void setSearch(const QString &str) noexcept;
          
          private:
              QString _search;
          
              bool filterAcceptsRow(int source_row, const QModelIndex &) const override;
          };
          

          sample.cpp

          bool StatusFilter::filterAcceptsRow(int source_row, const QModelIndex &) const
          {
              QStringList sl= _search.split(QRegExp("[ ,]+"),QString::SkipEmptyParts);
              for (auto const &s:sl)
              {
                  bool found= false;
                  for (int col:
                      {
                          static_cast<int>(STATUS_COLUMN::RMA_NUMBER),
                          static_cast<int>(STATUS_COLUMN::CUSTOMER_NAME),
                          static_cast<int>(STATUS_COLUMN::LOCATION),
                          static_cast<int>(STATUS_COLUMN::NOTES)
                      })
                  {
                      QModelIndex ix= sourceModel()->index(source_row,col);
                      QString str= ix.data().toString().toLower();
                      if (str.contains(s))
                          return true;
                  }
              }
              return false;
          }
          
          void StatusFilter::setSearch(const QString &str) noexcept
          {
              _search= str.toLower();
              invalidate();
          }
          
          1 Reply Last reply
          0
          • T Offline
            T Offline
            tako
            wrote on last edited by
            #5

            @tako said:

            I just want to appear in the folder I want with qtreeview . This sample code How will it help me?

            M 1 Reply Last reply
            0
            • T tako

              @tako said:

              I just want to appear in the folder I want with qtreeview . This sample code How will it help me?

              M Offline
              M Offline
              mjsurette
              wrote on last edited by
              #6

              @tako
              Your question lacks detail. If you're having a problem understanding how filterAcceptsRow() works, please supply something you've tried and the problem you're having with it.

              1 Reply Last reply
              1
              • T Offline
                T Offline
                tako
                wrote on last edited by
                #7

                @mjsurette ok knk

                1 Reply Last reply
                0

                • Login

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