QTableView is extremely slow (even for only 3000 rows)

后端 未结 8 910
北海茫月
北海茫月 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: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; csetHorizontalHeaderItem(c,new QTableWidgetItem(rec.fieldName(c)));
                                      }
    
                                  TBL->setItem(r, c, new QTableWidgetItem(qry.value(c).toString()));
    
                                  }
    
                                }
                            }
    
                            db2.close();
                     }
    

提交回复
热议问题