PDA

View Full Version : QTreeView and SQL - Performance Advice



johnnyturbo3
2nd February 2011, 14:05
Hi,

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:
http://doc.qt.nokia.com/latest/itemviews-simpletreemodel.html

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?

kornicameister
2nd February 2011, 16:58
you have three nested for loops so I would say that parsing the results takes time...
maybe consider is it possible to put your code in the larger sql query
database engine will do the job quicker than the code itself and than you could just pick up values for specific cells of the QSqlQuery and do only one for loop