Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - QTreeView and SQL - Performance advice

Tags:

c++

sql

qt

I have an app where I want to show SQL query results in a hierarchical structure. I have something work that is based on this example.

The main part of my code where the tree nodes are created currently looks like this:

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's 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's 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 retrieving 2000 queries worth of data. Can anyone suggest another approach to the one I'm using now?

like image 892
nf313743 Avatar asked Feb 24 '26 12:02

nf313743


1 Answers

You should implement function hasChildren() and use lazy population of model data. You should basically read this article and documentation of QAbstractItemModel class (especially canFetchMore() function).

like image 55
Maciej Avatar answered Feb 27 '26 00:02

Maciej



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!