Search code examples
c++performanceqtqlineeditqcompleter

How do i optimise performance of QCompleter?


I have initialized QCompleter with QStringList. And this stringlist has more than 30,000 entries. I have connected to Qlineedit in my ui. No issues there. The problem lies in, whenever i type something in that qlineedit, as you may guess it the suggestion pop up very slow, as there are more than 30,000 entries. So i was wondering if is there any other method to improve the performance? like by using multithread or something like that. I am newbie to qt, I apologise if i made any error in implementing. Thank you

Edit: My problem differs from this question QCompleter for large models as i am not using QComboBox, i am using QLineEdit.


Solution

  • The first thing you could try is using sorted QStringList instead of unsorted one. I don't know where you take the strings for your list but if you can get them in sorted order instead of unsorted one, you should definitely try it. Then you'd be able to call setModelSorting method on the completer with QCompleter::CaseSensitivelySortedModel or QCompleter::CaseInsensitivelySortedModel value - that would allow your completer to switch from linear search (O(n) complexity) to binary search (O(log(n)) complexity). Given your size of the string list, binary search in the worst case would require 11 comparisons (log(30000) ~= 10.3) to find the particular string while linear search would require 30000 comparisons in the worst case.

    It might be that this suggestion won't work for you because you can't get your data in sorted order within the string list (or into any other data structure represented by any custom QAbstractItemModel subclass). Unfortunately, it doesn't seem like QCompleter is easily extensible since its setCompletionPrefix method which seems to trigger the search for completions within the model is not virtual so you can't override it to work polymorphically. You might need to customize the widget for which you are attempting to use the completer so that it uses custom completion logic with efficient search and data structures and only uses QCompleter to hold a small (and sorted) list of already found candidates. There's an example which does somewhat similar thing so it might be worth checking out.