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
Forum Update on Monday, May 27th 2025

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.
  • A Offline
    A Offline
    andre
    wrote on 28 Nov 2011, 16:32 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 28 Nov 2011, 21:25 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 29 Nov 2011, 06:55 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 29 Nov 2011, 10:05 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 29 Nov 2011, 12:17 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 29 Nov 2011, 12:27 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 29 Nov 2011, 13:35 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 29 Nov 2011, 16:49 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 29 Nov 2011, 16:51 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 30 Nov 2011, 03:07 last edited by
                      #17

                      Ok, Thanks!

                      1 Reply Last reply
                      0

                      17/17

                      30 Nov 2011, 03:07

                      • Login

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