What's the most efficient way to update QListWidget when QLineEdit changes - filtering and restoring items in Qt?
-
I have a Qt desktop application with a QLineEdit that filters items in a QListWidget. I need to handle two scenarios:
- When user types/changes text in QLineEdit:
- Need to update QListWidget to show filtered items
- When user clears the QLineEdit:
- Need to restore/show all items
I'm considering these approaches:
- Clear the entire QListWidget and repopulate it from scratch each time
- Use setHidden()/setVisible() on items
Questions:
Which approach would be more performant, especially with large datasets?
Is there a better alternative I haven't considered?Environment:
- Qt 6.x
- Platform: Windows
Any insights on best practices for this common UI pattern would be appreciated.
- When user types/changes text in QLineEdit:
-
@YJMForgive said in What's the most efficient way to update QListWidget when QLineEdit changes - filtering and restoring items in Qt?:
, I want to filter items in a QListWidget based on what the user types in a QLineEdit. When users type in the QLineEdit, the QListWidget should show only matching items. Users should still be able to click on any visible list item to trigger corresponding events.
It's just that this is exactly what
QCompleter
does, on a list in a popup rather than in aQLisWidget
. And you can react on user clicking an item in the filtered list.Anyway, accepting that
QCompleter
is not what you want for whatever reason, it uses the approach of a model and a filter in the same way as described in @Pl45m4's suggestion. So you should follow that. -
@YJMForgive Are you aware of QCompleter, which already behaves this way?
-
@JonB I'm not looking for QLineEdit autocompletion. Instead, I want to filter items in a QListWidget based on what the user types in a QLineEdit. When users type in the QLineEdit, the QListWidget should show only matching items. Users should still be able to click on any visible list item to trigger corresponding events.
-
For this you should look into Qt's filter models...
E.g.
To use such model/view behavior you probably have to put
QListWidget
aside and use your own implementation ofQListView
with your custom model.for a proper filter model, start here:
The filter arguments can be taken from your line edit and the filtering can be updated on every
textChanged
signal emitted. -
@YJMForgive said in What's the most efficient way to update QListWidget when QLineEdit changes - filtering and restoring items in Qt?:
, I want to filter items in a QListWidget based on what the user types in a QLineEdit. When users type in the QLineEdit, the QListWidget should show only matching items. Users should still be able to click on any visible list item to trigger corresponding events.
It's just that this is exactly what
QCompleter
does, on a list in a popup rather than in aQLisWidget
. And you can react on user clicking an item in the filtered list.Anyway, accepting that
QCompleter
is not what you want for whatever reason, it uses the approach of a model and a filter in the same way as described in @Pl45m4's suggestion. So you should follow that. -