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:
{
// No more children => The parent is the last item
if (model->rowCount(parent) == 0) return parent;
QModelIndex lastChild
= model
->index
(model
->rowCount
(parent
)-1,
0,parent
);
return lastIndex(model,lastChild);
}
int MyClass::treeViewHeight() const
{
QRect r
= _treeView
->visualRect
(lastIndex
(_treeView
->model
(),_treeView
->rootIndex
()));
return r.y()+r.height();
}
QModelIndex MyClass::lastIndex(QAbstractItemModel *model, const QModelIndex &parent) const
{
// No more children => The parent is the last item
if (model->rowCount(parent) == 0) return parent;
QModelIndex lastChild = model->index(model->rowCount(parent)-1,0,parent);
return lastIndex(model,lastChild);
}
int MyClass::treeViewHeight() const
{
QRect r = _treeView->visualRect(lastIndex(_treeView->model(),_treeView->rootIndex()));
return r.y()+r.height();
}
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 ^^
Bookmarks