如何优化QCompleter的性能?(How do i optimise performance of QCompleter?)

编程入门 行业动态 更新时间:2024-10-28 03:25:57
如何优化QCompleter的性能?(How do i optimise performance of QCompleter?)

我用QStringList初始化了QCompleter。 此字符串列表包含超过30,000个条目。 我已经在我的ui中连接到Qlineedit了。 那里没有问题。 问题在于,每当我在qlineedit中键入内容时,你可能会猜到它的建议弹出非常慢,因为有超过30,000个条目。 所以我想知道是否还有其他方法来改善性能? 比如使用多线程或类似的东西。 我是qt的新手,如果我在实施中犯了任何错误,我道歉。 谢谢

编辑:我的问题不同于这个问题QCompleter对于大型模型,因为我没有使用QComboBox,我正在使用QLineEdit。

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.

最满意答案

您可以尝试的第一件事是使用排序的QStringList而不是未排序的QStringList 。 我不知道你在哪里为你的列表取字符串,但如果你可以按排序顺序而不是未排序的顺序,你应该尝试它。 然后你可以使用QCompleter::CaseSensitivelySortedModel或QCompleter::CaseInsensitivelySortedModel值在完成者上调用setModelSorting方法 - 这将允许你的完成者从线性搜索 (O(n)复杂度)切换到二进制搜索 (O(log( n))复杂性)。 给定字符串列表的大小,在最坏的情况下二进制搜索需要11次比较(log(30000)〜= 10.3)才能找到特定的字符串,而线性搜索在最坏的情况下需要30000次比较。

这可能是因为您无法在字符串列表中(或由任何自定义QAbstractItemModel子类表示的任何其他数据结构)中按顺序获取数据,因此此建议对您不起作用。 不幸的是,看起来QCompleter似乎不易扩展,因为它的setCompletionPrefix方法似乎触发了模型中的完成搜索并不是虚拟的,所以你不能覆盖它以多态方式工作。 您可能需要自定义要尝试使用完成QCompleter的窗口小部件,以便它使用具有高效搜索和数据结构的自定义完成逻辑,并仅使用QCompleter来保存已找到的候选项的小(和已排序)列表。 有一个例子有点类似的东西,所以值得一试。

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.

更多推荐

本文发布于:2023-07-28 04:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1300371.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:性能   QCompleter   performance   optimise

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!