Results 1 to 2 of 2

Thread: [QTreeView] Fit to height

  1. #1
    Join Date
    Feb 2010
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt Jambi
    Platforms
    MacOS X Unix/X11 Windows

    Default [QTreeView] Fit to height

    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 ^^

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: [QTreeView] Fit to height

    You have count the visible items and add each items height then viewport margin, then set that height to treeview.

    height = sum of height of all visible items (both child, sub-child... etc) + viewport margin (couple of pixels)
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. QTreeView height issue
    By mqt in forum Qt Programming
    Replies: 2
    Last Post: 23rd October 2013, 08:36
  2. row height with QTreeView and delegates
    By Philipp in forum Qt Programming
    Replies: 7
    Last Post: 11th November 2008, 11:40
  3. row height in QTreeView and QTableView
    By yuriry in forum Qt Programming
    Replies: 2
    Last Post: 9th October 2008, 01:58
  4. How to determine the header row height in QTreeView
    By ghorwin in forum Qt Programming
    Replies: 3
    Last Post: 20th September 2007, 20:39
  5. setting row height in QTreeView?
    By gfunk in forum Qt Programming
    Replies: 3
    Last Post: 28th July 2006, 01:47

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.