【问题标题】:QCombobox works very slow with QSqlQueryModel with large modelQCombobox 与大型模型的 QSqlQueryModel 一起工作很慢
【发布时间】:2015-09-23 23:05:18
【问题描述】:

我有几个组合框,其中包含大约 100K 行甚至更多的非常挖掘的数据集。我用QStandardItemModel 尝试过——如果模型是预加载的,那么工作速度足够快,如果在单独的线程中执行模型加载也需要几秒钟。尝试使用QSqlQueryModel 的组合框而不使用线程来提高性能,但体验到它的工作速度比QStandardItemModel 慢得多(例如,在我们的项目中,QSqlQueryModel 使用QTreeView 处理如此大量的数据非常快)。这里可能是什么问题?有没有办法加速组合框,一些参数?

附: Qt doc QComboBox::AdjustToMinimumContentsLengthWithIcon 的建议并没有加快速度:与此类组合的对话开始时间过长并退出 10-20 秒。 AdjustToMinimumContentsLength 工作得快一点,但无论如何延迟太长了。

【问题讨论】:

  • 你真的需要一个包含这么多项目的组合框吗?
  • 是的,用户应该从预定义的值中选择一个项目:一些 EMR 值集 - 包含大约 100k 个问题列表的医疗应用程序。
  • 问题可能出在所有模型的单个 QSqlDatabase 对象中吗?我应该为每个 QSqlQueryModel 使用单独的 QSqlDatabase 实例吗?
  • 我也有同样的问题。我认为 QComboBox 会创建所有数据视图,即使它们没有显示。
  • Paul-Sebastian,您可以在此处找到完整解决方案:stackoverflow.com/a/33454284/630169

标签: performance model-view-controller qt4 qcombobox


【解决方案1】:

找到了解决方案。第一个想法是找到哪个模型运行得更快,例如 QStringListModel 替换 QStandardItemModelQSqlQueryModel。但是似乎它们的工作速度几乎相同。第二,我在 Qt 文档中发现组合框默认使用 QStandardItemModel 来存储项目,QListView 子类显示弹出列表。您可以直接访问模型并查看(使用model()view())。这对我来说很奇怪,因为我知道QTreeView 可以很好地处理更大的数据量,更简单的QListView 也继承自QAbstractItemView 也应该这样做。我开始深入研究QListView,并在其中找到了解决问题的属性:现在组合框在大量数据上立即打开。编写静态函数来调整所有此类组合(带有解释的 cmets 来自 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.
}

【讨论】:

  • 只剩下一个问题:如何加速完成器或让它在第二个或第三个输入字符后打开?
  • 完整解决方案可以在这里找到:stackoverflow.com/a/33454284/630169
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-26
  • 1970-01-01
  • 2020-10-21
  • 2014-03-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多