Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Reimplementing QSortFilterProxyModel class
QtWS25 Last Chance

Reimplementing QSortFilterProxyModel class

Scheduled Pinned Locked Moved Qt Creator and other tools
17 Posts 3 Posters 9.9k 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.
  • F Offline
    F Offline
    freecamellia
    wrote on last edited by
    #6

    Many thanks for replying.
    Please see this image to understand what I need (sort and filter) as follow:

    !http://im12.gulfup.com/2011-11-28/1322496139191.png(photo)!

    What I need to do?

    1 Reply Last reply
    0
    • G Offline
      G Offline
      goetz
      wrote on last edited by
      #7

      No reimplementation of sort filter methods is needed.

      Set the proxy model, set sorting enabled in your list view.

      connect textChanged() signal of the line edit to setFilterRegExp. If you need to search in the middle, set teh wildcard with setFilterWildcard.

      I suggest you start with the snippets in the docs and add functionality step by step.

      Again: you do not need to reimplement neither lessThan nor filterAcceptsRow.

      http://www.catb.org/~esr/faqs/smart-questions.html

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #8

        Oh, and if you want to prevent blocking the UI while the user is typing because filtering takes too long, considder using "this":http://developer.qt.nokia.com/wiki/Delay_action_to_wait_for_user_interaction :-) It was written (by me) for exactly this use case.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          freecamellia
          wrote on last edited by
          #9

          Thanks for you two.

          bq. Set the proxy model.

          @QStringList termList;
          termList << "a" << "ab" << "ade" << "cde" << "Qt";

          QStringListModel *termListModel = new QStringListModel;
          termListModel->setStringList(termList);

          QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel;
          proxyModel->setSourceModel(termListModel);

          ui->termListView->setModel(proxyModel);@

          bq. set sorting enabled in your list view.

          My listView termListView haven't a method that set sorting enable!

          bq. connect textChanged() signal of the line edit to setFilterRegExp.

          setFilterRegExp is a slot of what object?

          Sorry and be patient :)

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #10

            Yes, you are right. A QListView doesn't have that method. Only QTableView and QTreeView do. The reason is that QListView does not have headers to control the sorting. However, you can enable the sorting directly on the proxy model by calling the sort(int column, Qt::SortOrder ) method.

            Both setFilterRegExp and sort are methods of QSortFilterProxyModel.

            1 Reply Last reply
            0
            • F Offline
              F Offline
              freecamellia
              wrote on last edited by
              #11

              It works by adding these lines:

              @proxyModel->sort(0);
              ...
              connect(ui->termLine, SIGNAL(textChanged(QString)), proxyModel, SLOT(setFilterRegExp(QString)));@

              but I wont the filter system search in the entire QString, just from the beginning.
              for example if I type a letter 'a', the result in the listView will "a", "a b", "a bc", but not "b a c"

              Thanks, and I hope you understand me.

              1 Reply Last reply
              0
              • G Offline
                G Offline
                goetz
                wrote on last edited by
                #12

                Then you need a proxy slot, that constructs the proper regular expression:

                @
                connect(ui->termLine, SIGNAL(textChanged(QString)), this, SLOT(setTreeViewFilter(QString)));

                void MyClass::setTreeViewFilter(const QString &filter)
                {
                proxyModel->setFilterRegExp(QString("^%1").arg(filter));
                }
                @

                http://www.catb.org/~esr/faqs/smart-questions.html

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #13

                  [quote author="freecamellia" date="1322561104"]but I wont the filter system search in the entire QString, just from the beginning.
                  for example if I type a letter 'a', the result in the listView will "a", "a b", "a bc", but not "b a c"[/quote]

                  I already gave you the code for that a few postings ago.

                  1 Reply Last reply
                  0
                  • F Offline
                    F Offline
                    freecamellia
                    wrote on last edited by
                    #14

                    @Volker & @Andre, pleased to meet you. It work !
                    Thanks a lot.

                    1 Reply Last reply
                    0
                    • F Offline
                      F Offline
                      freecamellia
                      wrote on last edited by
                      #15

                      What's means @"^%1"@ I know it's a regular expression but what it represent?

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #16

                        The %1 will be replaced with the contents of filter (the first argument). The ^ is the begin-of-string marker.

                        1 Reply Last reply
                        0
                        • F Offline
                          F Offline
                          freecamellia
                          wrote on last edited by
                          #17

                          Ok, Thanks!

                          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