QCompleter for large models

ぐ巨炮叔叔 提交于 2020-01-21 15:07:38

问题


QCompleter works slightly slow on large data sets (large models): when I start to input characters in QCombobox it passes few seconds to show auto-complete popup with variants, when input 2nd char QCompleter does not react on key press for few seconds as well. Next characters works fine. Model size is about 100K records. Is it possible to improve QCompleter performance or show popup after 2nd or 3rd input symbol? Are there some good examples?


回答1:


Solution appears similar to this: https://stackoverflow.com/a/33404207/630169 as QCompleter also uses QListView in its popup(). So full solution to speed-up QCombobox is:

Tweak combo:

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());
}

Tweak dropdown/popup (view):

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);
}

Tweak completer:

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());
}


来源:https://stackoverflow.com/questions/33447843/qcompleter-for-large-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!