Hi All,
I want to implement a filter for QTreeView.
-Apple
|____ Object_01
|____ Object_02
|____ XYZ
-Banana
|____ Object_03
|____ Lemon
- Orange
|____ Rabbit
|____ Object_04
|____ Object_05
-Apple
|____ Object_01
|____ Object_02
|____ XYZ
-Banana
|____ Object_03
|____ Lemon
- Orange
|____ Rabbit
|____ Object_04
|____ Object_05
To copy to clipboard, switch view to plain text mode
I have to use a LineEdit for dynamic filter. (Connection to textchange I have done)
Conditions:
1> With every Stroke of letter whole tree should be filtered.
e.g. If in lineEdit I say Obj it should show, important thing is every word in the list should start with Obj.
-Apple
|____ Object_01
|____ Object_02
-Banana
|____ Object_03
- Orange
|____ Object_04
|____ Object_05
-Apple
|____ Object_01
|____ Object_02
-Banana
|____ Object_03
- Orange
|____ Object_04
|____ Object_05
To copy to clipboard, switch view to plain text mode
I tried using simple filter example(BasisSortFilter) it works perfectly fine for tree with only parents and no children. However it's not working with tree with childrens (above example)
This quote I read while going through the documentation of qsortfilterproxymodel.
For hierarchical models, the filter is applied recursively to all children. If a parent item doesn't match the filter, none of its children will be shown.
This slot is connected to lineEdit with
connect(FilterLineEdit,
SIGNAL(textChanged
(QString)),
this,
SLOT(filterChanged
()));
connect(FilterLineEdit, SIGNAL(textChanged(QString)),this, SLOT(filterChanged()));
To copy to clipboard, switch view to plain text mode
void FilterDialog::filterChanged()
{
Qt::CaseSensitivity caseSensitivity = Qt::CaseInsensitive;
QString strPattern
= "^" + FilterLineEdit
->text
();
QRegExp regExp
(strPattern, caseSensitivity
);
proxyModel->setFilterRegExp(regExp);
}
void FilterDialog::filterChanged()
{
QRegExp::PatternSyntax syntax = QRegExp::RegExp;
Qt::CaseSensitivity caseSensitivity = Qt::CaseInsensitive;
QString strPattern = "^" + FilterLineEdit->text();
QRegExp regExp(strPattern, caseSensitivity);
proxyModel->setFilterRegExp(regExp);
}
To copy to clipboard, switch view to plain text mode
Thanks in advance.
Bookmarks