title: Qt - QTreeView and SQL - Performance advice
body: <p>I have an app where I want to show SQL query results in a hierarchical structure.  I have something work that is based on <a href="http://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html" rel="nofollow">this example</a>.</p>
  
  <p>The main part of my code where the tree nodes are created currently looks like this:</p>
  
  <pre><code>void TreeModel::setupModelData(TreeItem *parent)
  {
    QList&lt;TreeItem*&gt; parents;
    QList&lt;int&gt; indentations;
    parents &lt;&lt; parent;     
    QList&lt;QVariant&gt; columnData;
  
    QVector&lt;QString&gt; vecFileNames = getFileNames();
    QVector&lt;QString&gt; vecTableNames = getTableNames();
  
    for(int i = 0; i &lt; vecFileNames.size(); i++)
    {
      columnData.clear();
      columnData &lt;&lt; vecFileNames[i];
      parents.last()-&gt;appendChild(new TreeItem(columnData, parents.last()));
  
      int childCount = parents.last()-&gt;childCount() - 1;
      parents &lt;&lt; parents.last()-&gt;child(childCount);    //add the current parent's last child as a parent
  
      for(int j = 0; j &lt; vecTableNames.size(); j++)
      {
        columnData.clear();
        columnData &lt;&lt; vecTableNames[j];
        parents.last()-&gt;appendChild(new TreeItem(columnData, parents.last()));
  
        QVector&lt;QString&gt; vecTableValues = getTableValues(&amp;vecTableNames[j]);
        int childCount = parents.last()-&gt;childCount() - 1;
        parents &lt;&lt; parents.last()-&gt;child(childCount);         //add the current parent's last child as a parent
  
        for(int k = 0; k &lt; vecTableValues.size(); k++)
        {
          columnData.clear();
          columnData &lt;&lt; vecTableValues[j];
          parents.last()-&gt;appendChild(new TreeItem(columnData, parents.last()));
        }
  
      }
      parents.pop_back();
    }
  
  }
  
  QVector&lt;QString&gt; TreeModel::getFileNames()
  {
    db.open();
  
    QVector&lt;QString&gt; 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;
  }
  </code></pre>
  
  <p>However, it is incredibly slow retrieving 2000 queries worth of data.
  Can anyone suggest another approach to the one I'm using now?</p>