QCompleter适用于大型模型

时间:2015-10-31 03:48:33

标签: qt model qcompleter large-data

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

1 个答案:

答案 0 :(得分:5)

解决方案与此类似:https://stackoverflow.com/a/33404207/630169由于QCompleter在其QListView中也使用了popup()。所以加速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());
}