Limit total number of completions in QCompleter
How can I limit the number of completions in QCompleter, such as with this example https://doc.qt.io/qt-5/qtwidgets-tools-customcompleter-example.html , such that only a specific number of closest matches are shown (not just the ones visible without scrolling)?
Isn't QCompleter's maxVisibleItems property what you are looking for ?
@SGaist No, since that doesn't prevent more than that being shown on the item view altogether.
JonB last edited by
I did look at this yesterday, and did understand you were not talking about the visible items.
I presume your use case is that you have lots of potential completions available, and it is "expensive" to calculate/return them all, so you want to restrict how many are returned.
Not that I know exactly how to do this, but since I do not see any in-built support for this in
QCompletermethods I assume you must do this yourself. In some shape or form, interpose a
QCompleterand your actual data, so that it only returns to the completer the maximum number of items you desire. You may (well) find you need to act on user typing each search character, so that you can adjust the hits returned depending on what user has typed so far.
@JonB Yes. The best method I've found so far is to take the completion model, and when the completable text updates, create a copy of the completionModel and limit the rowCount in that model. It's quite inefficient though since a copy could probably be prevented in some other way.
JonB last edited by
create a copy of the completionModel
I admit I haven't looked at how it can be coded, but wouldn't a proxy model (with a limited
rowCount()return) obviate any copying of the model data?
@JonB Creating a proxy model from the QCompleter's completionModel doesn't work, since the QCompleter destroys the model the proxy model is trying to use, and you get a segfault.
Lurkki last edited by Lurkki
I ended up creating a QSortFilterProxyModel derived class with a limited row count and set that as the model for QCompleter and do the searching/filtering with that model instead of QCompleter, so the relationship goes as follows:
Original model <-> Proxy model -> QCompleter ^ | QTextEdit