QCombobox与大型模型的QSqlQueryModel一起运行非常慢 [英] QCombobox works very slow with QSqlQueryModel with large model

查看:435
本文介绍了QCombobox与大型模型的QSqlQueryModel一起运行非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很少的组合框,这些组合框的挖掘数据集在约10万行之内,甚至还有更多.我用QStandardItemModel进行了尝试-如果预加载了模型,则运行速度足够快,如果在单独的线程中执行,则模型加载也需要几秒钟.在没有线程的情况下尝试使用QSqlQueryModel的组合框可以提高性能,但体验起来比QStandardItemModel慢得多(例如,在我们的项目QSqlQueryModel中,使用QTreeView处理如此大量的数据非常快).这可能是什么问题?有没有一种方法可以加快组合框的速度,增加一些参数?

I have few comboboxes with very dig data sets within ~ 100K rows and more. I tried it with QStandardItemModel - works fast enough if model is preloaded, also model loading takes few seconds if performed in separate thread. Tried comboboxes with QSqlQueryModel without threading to improve performance but experienced it works much slower than QStandardItemModel (in our project QSqlQueryModel works very fast with such amount of data with QTreeView for example). What could be the problem here? Is there a way to speed-up combobox, some parameters?

P.S. Qt doc QComboBox::AdjustToMinimumContentsLengthWithIcon建议的速度不会很快:具有此类连击的对话框开始时间过长,并退出10到20秒. AdjustToMinimumContentsLength的工作速度更快,但是延迟太长了.

P.S. Suggested by Qt doc QComboBox::AdjustToMinimumContentsLengthWithIcon does not speed things much: dialog with such combos starts too long and exits 10-20 sec. AdjustToMinimumContentsLength works a little bit faster but anyway delays are too long.

推荐答案

找到了解决方案.第一个想法是找到哪种模型可以更快地工作,例如用QStringListModel代替QStandardItemModelQSqlQueryModel.但是似乎它们的工作速度几乎相同.第二,我在Qt文档中发现,默认情况下,组合框使用QStandardItemModel存储项目,而QListView子类显示弹出列表.您可以访问模型并直接查看(使用model()view()).这对我来说很奇怪,因为我知道QTreeView可以在更大的数据量下正常工作,并且从QAbstractItemView继承的更简单的QListView也应该这样做.我开始研究QListView并找到解决问题的属性:现在,组合框会立即打开大量数据.编写了静态函数来调整所有这些组合(注释的解释来自Qt doc):

Found the solution. 1st thought was to find what model will work faster, for example QStringListModel to replace QStandardItemModel or QSqlQueryModel. However seems that they work almost same speed. 2nd I found in Qt doc that combobox by default uses QStandardItemModel to store the items and a QListView subclass displays the popuplist. You can access the model and view directly (with model() and view()). That was strange for me as I know QTreeView works just fine with even bigger amount of data, and simpler QListView also inherited from QAbstractItemView should do this as well. I start to digg into QListView and found properties in it which had solved the problem: now combobox opens immediately on large amount of data. Static function was written to tweak all of such combos (comments with explanation are from Qt doc):

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

  QListView *view = (QListView *)combo->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.
}

这篇关于QCombobox与大型模型的QSqlQueryModel一起运行非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆