DAStringFilterProxyModel¶
DAStringFilterProxyModel is a general text-filtering proxy for lists and trees: it matches Qt::DisplayRole (or a custom role), is case-insensitive by default, can be restricted to a column, and — for tree models — can keep the parents of matching children visible.
Preview¶

The tree has three groups. Typing a filter (for example ban, 胡萝, or wrench) keeps only matching leaves and their parent chain; clearing the input restores everything. The demo shows the filtered top-level count versus the source count below.
Key Features¶
- ✅ Display-text filtering:
Qt::DisplayRoleby default; change it withsetFilterRole() - ✅ Case policy:
setFilterCaseSensitivity(), insensitive by default - ✅ Single/all columns:
setFilterColumn(-1)matches all columns - ✅ Parent pass-through:
setIncludeParentsOfMatches(true)keeps parents of matching children - ✅ Reusable matching: static
matchesText()shares the exact semantics with views that cannot use a proxy - ✅ Match ranges:
matchRanges()/matchRangesIn()so a delegate can paint its own highlights
Usage¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
Enable includeParentsOfMatches for trees
The proxy checks each row on its own by default. If a parent's text does not contain the filter term, the parent is hidden and its children become unreachable. For tree data, set setIncludeParentsOfMatches(true).
Match Ranges and Highlighting¶

Range Semantics¶
1 2 3 4 5 | |
start/lengthare UTF-16 code unit offsets;text.mid(start, length)is the matched substring.- Ranges are left-to-right and never overlap:
"aaaa"+"aa"yields{(0,2),(2,2)}, not three ranges. - An empty filter returns an empty vector (nothing highlighted), while
matchesText()returnstruefor an empty filter (everything passes) — "passes" and "highlighted" are different questions. - With
setFilterColumn(n), only that column has ranges; invalid indexes or indexes that do not belong to this proxy return an empty vector. - In trees, a parent kept only because a child matches has no ranges of its own (its own text truly did not match).
- Ranges come from the
filterRoletext. Only when that text equals the displayedDisplayRoletext can the offsets be painted directly; otherwise they will be misaligned.
Minimal Delegate Example¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
- The complete implementation (baseline, clipping, fallbacks) is the demo's
filter-highlightscene; the library does not ship a highlight delegate, leaving selection colors, elision and styling to the caller. - Computing ranges inside every
paint()is fine:setFilterText()invalidates the filter and the view repaints with fresh values.
Core Methods¶
| Method | Description |
|---|---|
setFilterText(QString) / filterText() |
Filter text (empty = no filtering) |
setFilterCaseSensitivity(Qt::CaseSensitivity) |
Case policy |
setFilterColumn(int) |
Column to match (-1 = all columns) |
setFilterRole(int) |
Data role to match (default Qt::DisplayRole) |
setIncludeParentsOfMatches(bool) |
Keep parents of matching children |
matchesText(text, filter, cs) |
Static matcher with identical semantics |
matchRanges(proxyIndex) |
Match ranges (start, length) for one cell, for delegate highlighting |
matchRangesIn(text, filter, cs) |
Pure-text ranges; an empty filter returns an empty vector |
References¶
- DACheckListWidget
- Example path:
examples/all_widgets_demo(Filter tab)