Hi,

thx to amicitas and other posts in this thread I was able to build a tree from sql. But I am not using a model: On startup i execute a query for the parent items (children of the rootitem). My problem was, that I want to load only the data form the database that are visible at the beginning otherwise the whole thing is too slow. When the user clicks a parent (all of them have children) I execute another query to load the childitems. This is quite fast and works. My problem is, that i am not able to connect to the expanded() signal of the treeView. Right now I am hiding the decoration (setRootIsDecorated(false)) and use clicks on items within the view to expand or collapse. The expanding is done within the model ...
Qt Code:
  1. connect(ui.treeView, SIGNAL(clicked(QModelIndex)), model, SLOT(expand(QModelIndex)));
  2.  
  3. connect(ui.treeView, SIGNAL(expanded(QModelIndex)), model, SLOT(expand(QModelIndex))); //<-- no way. infinite loop
  4.  
  5. void TreeModel::expand(const QModelIndex &index)
  6. {
  7. QModelIndex idx = proxy->mapToSource(index);
  8.  
  9. QModelIndex sourceIndex = idx.sibling(idx.row(), 0);
  10. QModelIndex proxyIndex = index.sibling(index.row(), 0);
  11.  
  12. if (!hasChildren(sourceIndex))
  13. return;
  14.  
  15. TreeItem *item = getItem(sourceIndex);
  16.  
  17. if (item->childCount() > 0) {
  18.  
  19. if (view->isExpanded(proxyIndex)) {
  20. view->collapse(proxyIndex);
  21. return;
  22. }
  23.  
  24. view->expand(proxyIndex);
  25.  
  26. }
  27.  
  28. else {
  29.  
  30.  
  31. .. execute query
  32.  
  33. setupModelData(q, item);
  34. view->expand(proxyIndex);
  35.  
  36. }
  37.  
  38. }
To copy to clipboard, switch view to plain text mode 

This is not a very nice solution. I'd rather prefer to use the default rootdecoration. But i can not find a way to get the expanded signal and load the data: Nothing is dispayed because the signal is emitted after the view is updated (At that point there is no data). If I call the expand slot again after the query I end up in an infinite loop ... I took a look at the Qt source code, but all parts that are required to cache the expansion (the click on the decoration [+] ) before the model is checked for children are part of QTreeViewPrivate. Sorry for the long explanation, but maybe someone has an idea :-)