Reimplementing QSortFilterProxyModel class
-
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!
-
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?
-
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?
-
not I have created, but I have to create! sorry!
-
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));
}
@ -
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!