【问题标题】:QCompleter for large models大型模型的 QCompleter
【发布时间】:2016-01-31 13:53:27
【问题描述】:

QCompleter 在大型数据集(大型模型)上工作稍慢:当我开始在QCombobox 中输入字符时,它会经过几秒钟来显示带有变体的自动完成弹出窗口,当输入第二个字符 QCompleter 时不会按键反应几秒钟。下一个字符工作正常。模型大小约为 100K 记录。是否可以提高QCompleter 的性能或在第二个或第三个输入符号后显示弹出窗口?有什么好的例子吗?

【问题讨论】:

  • 你用什么型号的?可能,您需要提高模型的性能。
  • 我使用QStandardItemModel。如何提高其性能?我尝试对其进行预排序,以设置灵敏度,如 Qt doc 所说:combo->completer()->setCaseSensitivity(Qt::CaseInsensitive); combo->completer()->setModelSorting(QCompleter::CaseSensitivelySortedModel); 我还能做什么?
  • 您可以根据QAbstractItemModel创建自己的模型。
  • 不,QStandardItemModel这里够用了,看看下面的答案。

标签: qt model qcompleter large-data


【解决方案1】:

解决方案与此类似:https://stackoverflow.com/a/33404207/630169 as QCompleter 在其popup() 中也使用了QListView。所以 完整 加速 QCombobox 的解决方案是:

调整组合:

void ComboboxTools::tweak(QComboBox *combo)
{
  //  For performance reasons use this policy on large models
  // or AdjustToMinimumContentsLengthWithIcon
  combo->setSizeAdjustPolicy(QComboBox::AdjustToMinimumContentsLength);

  // Improve combobox view performance
  tweak((QListView *)combo->view());

  // Improve combobox completer performance
  tweak(combo->completer());
}

调整下拉/弹出(视图):

void ComboboxTools::tweak(QListView *view)
{
  // Improving Performance:  It is possible to give the view hints
  // about the data it is handling in order to improve its performance
  // when displaying large numbers of items. One approach that can be taken
  // for views that are intended to display items with equal sizes
  // is to set the uniformItemSizes property to true.
  view->setUniformItemSizes(true);
  // This property holds the layout mode for the items. When the mode is Batched,
  // the items are laid out in batches of batchSize items, while processing events.
  // This makes it possible to instantly view and interact with the visible items
  // while the rest are being laid out.
  view->setLayoutMode(QListView::Batched);
  // batchSize : int
  // This property holds the number of items laid out in each batch
  // if layoutMode is set to Batched. The default value is 100.
  // view->setBatchSize(100);
}

调整完成者:

void ComboboxTools::tweak(QCompleter *completer)
{
  completer->setCaseSensitivity(Qt::CaseInsensitive);
  // If the model's data for the completionColumn() and completionRole() is sorted
  // in ascending order, you can set ModelSorting property
  // to CaseSensitivelySortedModel or CaseInsensitivelySortedModel.
  // On large models, this can lead to significant performance improvements
  // because the completer object can then use a binary search algorithm
  // instead of linear search algorithm.
  completer->setModelSorting(QCompleter::CaseSensitivelySortedModel);

  // Improve completer popup (view) performance
  tweak((QListView *)completer->popup());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 1970-01-01
    • 2011-04-18
    相关资源
    最近更新 更多