Qt - QTreeView and SQL - performance recommendations

I have an application in which I want to show the results of SQL queries in a hierarchical structure. I have work based on in this example .

The bulk of my code, in which the tree nodes are being created, is as follows:

void TreeModel::setupModelData(TreeItem *parent)
{
  QList<TreeItem*> parents;
  QList<int> indentations;
  parents << parent;     
  QList<QVariant> columnData;

  QVector<QString> vecFileNames = getFileNames();
  QVector<QString> vecTableNames = getTableNames();

  for(int i = 0; i < vecFileNames.size(); i++)
  {
    columnData.clear();
    columnData << vecFileNames[i];
    parents.last()->appendChild(new TreeItem(columnData, parents.last()));

    int childCount = parents.last()->childCount() - 1;
    parents << parents.last()->child(childCount);    //add the current parent last child as a parent

    for(int j = 0; j < vecTableNames.size(); j++)
    {
      columnData.clear();
      columnData << vecTableNames[j];
      parents.last()->appendChild(new TreeItem(columnData, parents.last()));

      QVector<QString> vecTableValues = getTableValues(&vecTableNames[j]);
      int childCount = parents.last()->childCount() - 1;
      parents << parents.last()->child(childCount);         //add the current parent last child as a parent

      for(int k = 0; k < vecTableValues.size(); k++)
      {
        columnData.clear();
        columnData << vecTableValues[j];
        parents.last()->appendChild(new TreeItem(columnData, parents.last()));
      }

    }
    parents.pop_back();
  }

}

QVector<QString> TreeModel::getFileNames()
{
  db.open();

  QVector<QString> vecFileNames;
  QSqlQuery query(db);
  QString strQuery = "SELECT PK_fileName FROM fileproperties";
  query.prepare(strQuery);

  if(query.exec() == true)
  {
    while(query.next())
    {
      vecFileNames.push_back(query.value(0).toString());
    }
  }

  db.close();
  return vecFileNames;
}

However, it is incredibly slow to receive 2,000 data requests. Can someone suggest a different approach to the one I'm using now?

+3
source share
3 answers
+1

MS SQL QSqlQuery:: setForward (true), 10 .

"" sql , ( ).

MS SQL Server 2005 + 2008 10 , 200-400 , QTableView QSqlTableModel.

"" 10 200-300 - 10 !

:

QSqlQuery query(database);
query.setForwardOnly(m_bForwardOnly);

query.exec(statement);
if ( query.lastError().isValid() || database.lastError().isValid() ) {
   ...evaluate the results...
}
0

I would suggest that performance lost by inserting 2,000 records separately, causing an update of 2,000 views, possibly 2,000 sorts, etc. .... you must provide a way to add data to your model that accepts batches of elements and only changes to signals once...

0
source

Source: https://habr.com/ru/post/1789504/


All Articles