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
    #1

    I want sort and filter items of an other model before put it in a view. What are methods of the QSortFilterProxyModel Class that must be reimplemented.

    For sorting, this reimplementation of the lessThan method is right?

    @bool MySortFilterProxyModel::lessThan(const QModelIndex &left,
    const QModelIndex &right) const
    {
    QVariant leftData = sourceModel()->data(left);
    QVariant rightData = sourceModel()->data(right);
    return leftData.toDateTime() < rightData.toDateTime();
    }
    @

    Thanks in advance!

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

      Yeah, that looks about right to me. The only issue is, that perhaps you need to check that you can actually convert the data to QDateTime's in a sane way before you compare the data points. Other than that, it looks fine to me.

      Do you have a problem with this code?

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

        I want only sort QString data, not QDateTime.

        I have created a lineEdit widget and a listView commanded (dynamically sorted and filtered) whenever the contents of the lineEdit change.

        Just I need to know What are methods of the QSortFilterProxyModel Class that must be reimplemented, and how to do this example?

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

          not I have created, but I have to create! sorry!

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

            If you only need to sort plain strings, there is no need to reimplement anything. The default implemetation will do just fine then.

            I guess the line edit is for filtering the list? For that part, you simply create a slot in your own class (perhaps the class the form itself is defined in) and connect that to the textChanged() signal of the QLineEdit. In that slot, you simply call the QSortFilterProxyModel's setFilterWildCard() method like this:
            @
            void filterChanged(const QString& newText) {
            theProxyModel->setFilterWildCard(QString("%1").arg(newText));
            }
            @

            1 Reply Last reply
            0
            • 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