QTableView is extremely slow (even for only 3000 rows)

后端 未结 8 902
北海茫月
北海茫月 2021-01-04 11:20

I have a table with 3000 rows and 8 columns. I use the QTableView. To insert items I do:

QStandardItem* vSItem = new QStandardItem();
vSItem->setText(\"Bl         


        
相关标签:
8条回答
  • 2021-01-04 11:32

    For this quantity of data, you'd be better with a custom model - then you'd have the control of when you inform the view of updates, for example. The 'standard' items scale to hundreds, and probably thousands, due to modern hardware being fast, but they're explicitly documented as not being intended for datasets of this size.

    0 讨论(0)
  • 2021-01-04 11:33

    I am using 80000 rows and had a similar problem adding huge amounts of items to a table.

    My solution was to let it allocate the memory in advanced by telling it how many rows it will need.

    I was using a Qtableview and model, so:

    self.model.setRowCount(80000)

    I'm sure you can match this up with your code

    0 讨论(0)
  • 2021-01-04 11:39

    Do you have an autoresize on contents for your columns or rows ? It can be a killer in performance sometimes !

    Have a look here : QHeaderView::ResizeToContents

    Hope it helps !

    0 讨论(0)
  • 2021-01-04 11:50

    try this :

                 QSqlDatabase db =QSqlDatabase::addDatabase( "QSQLITE");
    
          void SELECT_TO_TBLWID(QTableWidget * TBL, QString DbPath,QString SQL)
                      {
                          QSqlDatabase db2 =QSqlDatabase::database();
                          db2.setDatabaseName(DbPath);
                          if( !db2.open() )
                          {
                            qDebug() << db2.lastError();
                            qFatal( "Failed to connect." );
                          }
                            QSqlQuery qry;
                            qry.prepare(SQL);
                            if( !qry.exec() )
                              qDebug() << qry.lastError();
                            else
                            {
                               QSqlRecord rec = qry.record();
    
                                 TBL->setColumnCount(rec.count());
                                 int RW=0;
                                 for( int r=0; qry.next(); r++ )
                                    {RW++;}
                                    TBL->setRowCount(RW);
                                    for (int pr=RW;qry.previous();pr--){// do nothing}
    
                             for( int r=0; qry.next(); r++ )
                                {
    
                                  for( int c=0; c<rec.count(); c++ )
                                  {
                                      if ( r==0)
                                      {
                                          TBL->setHorizontalHeaderItem(c,new QTableWidgetItem(rec.fieldName(c)));
                                      }
    
                                  TBL->setItem(r, c, new QTableWidgetItem(qry.value(c).toString()));
    
                                  }
    
                                }
                            }
    
                            db2.close();
                     }
    
    0 讨论(0)
  • 2021-01-04 11:51

    I found a solution: the problem was that I assigned the model to the tableview already in the constructor. So everytime I inserted the item in the model, tableview was informed and probably updated. Now I assign the model to the tableview only after I filled my model with data. This is not an elegant solution but it works. Is there maybe a way to temporarily disable the model from tableview or something that says to the tableview to not to care about changes in the model?

    0 讨论(0)
  • 2021-01-04 11:53

    Also, if all your rows have the same height, setting http://doc.qt.io/qt-5/qtreeview.html#uniformRowHeights-prop to true can boost performance. In my case, a model containing about 50.000 rows was almost unusable with uniformRowHeights set to false (the default). After changing it to true, it worked like a charm.

    0 讨论(0)
提交回复
热议问题