QCombobox works very slow with QSqlQueryModel with large model

前端 未结 1 445
[愿得一人]
[愿得一人] 2021-01-07 01:44

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 mo

相关标签:
1条回答
  • 2021-01-07 02:37

    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.
    }
    
    0 讨论(0)
提交回复
热议问题