QCombobox 使用带有大模型的 QSqlQueryModel 工作非常慢 [英] QCombobox works very slow with QSqlQueryModel with large model

查看:25
本文介绍了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?

附言由 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 在处理更大数量的数据时工作得很好,而且更简单的 QListView 也继承自 QAbstractItemView 应该这样做以及.我开始深入研究 QListView 并在其中找到解决了问题的属性:现在组合框在大量数据上立即打开.编写了静态函数来调整所有此类组合(带有解释的注释来自 Qt 文档):

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天全站免登陆