Hi all,

I'm currently working on a GUI where multiple QTreeView are put into a single, parent QScrollArea.
Since a QTreeView inherits from QAbstractScrollArea, and in order to avoid showing multiple scroll bars, I'm looking to resize my QTreeView so that its height fits with its content.

I've found no solution so far that gives me the precise height I should set on my QTreeView so that no scrollbar is needed. Here is the solution that comes the closest to the result:

Qt Code:
  1. QModelIndex MyClass::lastIndex(QAbstractItemModel *model, const QModelIndex &parent) const
  2. {
  3. // No more children => The parent is the last item
  4. if (model->rowCount(parent) == 0) return parent;
  5. QModelIndex lastChild = model->index(model->rowCount(parent)-1,0,parent);
  6. return lastIndex(model,lastChild);
  7. }
  8.  
  9. int MyClass::treeViewHeight() const
  10. {
  11. QRect r = _treeView->visualRect(lastIndex(_treeView->model(),_treeView->rootIndex()));
  12. return r.y()+r.height();
  13. }
To copy to clipboard, switch view to plain text mode 

So basically I check for the visual rect of the last item in my model and try to resize the treeview accordingly. However, I still end up with one ore more rows (depending on the total number of items) that are not shown in the viewport.

Any help would be greatly appreciated ^^