Reimplementing QSortFilterProxyModel class
-
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?
-
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.
-
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.
-
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 :)
-
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.
-
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.
-
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));
}
@ -
[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.
-
What's means @"^%1"@ I know it's a regular expression but what it represent?
-
Ok, Thanks!