Results 1 to 12 of 12

Thread: QTreeWidget minimum height

  1. #1
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default QTreeWidget minimum height

    Hi,
    I have a layout with QTreeWidget but that expands I would have a fixed size to have the minimum size to show all item inside the tree.
    I have a QScrollArea with the layout inside it, the problem is I have the tree which takes all the place, I would have only the minimum space needed to show all items.
    I have tried a lot of way but no one worked correctly.
    How can I achieve this result ?
    Thanks for the help

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeWidget minimum height

    calculate the size (including the header and frames), return it from size hint and set the size policy to Fixed or Preferred.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeWidget minimum height

    The only way I found is to set the size policy :
    Qt Code:
    1. setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Maximum );
    To copy to clipboard, switch view to plain text mode 
    And use this sizeHint :
    Qt Code:
    1. QSize CPropertyEditorWidget::sizeHint() const
    2. {
    3. // Iterator of all items.
    4. QTreeWidgetItemIterator AllIterator( m_TreeWidget, QTreeWidgetItemIterator::All );
    5.  
    6. // Height of all item and header.
    7. int Height = m_TreeWidget->header()->height() + 2;
    8.  
    9. // While iterator is valid.
    10. while( *AllIterator )
    11. {
    12. // Update the height.
    13. QTreeWidgetItem* Item = *AllIterator;
    14. Height += m_TreeWidget->visualItemRect( Item ).height();
    15.  
    16. // Next iterator.
    17. ++AllIterator;
    18. }
    19.  
    20. // Return the size hint.
    21. return QSize( m_TreeWidget->sizeHint().width(), Height );
    22. }
    To copy to clipboard, switch view to plain text mode 
    There is a way to avoid the "+ 2" in this line ?
    Qt Code:
    1. int Height = m_TreeWidget->header()->height() + 2;
    To copy to clipboard, switch view to plain text mode 
    Thanks for the help

    EDIT:
    The code before is not perfect on all situation, I have changed using "setFixedHeight( Height );" when one item is added but if the widget use the size policy Expanding when one item is added the siz policy change to fixed on height because of "setFixedHeight( Height );", that looks like a not easy problem to solve.
    Last edited by Alundra; 18th November 2014 at 03:32.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeWidget minimum height

    +2 comes from the frame size. You should use QStyle::styleHint() to ask the style for the proper frame size of your style.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeWidget minimum height

    I have found an easy way to handle my problem, I have a SetFixedHeightToShowAllItems() and I set the size policy to fixed when needed.
    About the frame width I only found this one, all other are for specific widget but not the tree :
    Qt Code:
    1. const int FrameWidth = m_TreeWidget->style()->styleHint( QStyle::StyleHint::SH_WindowFrame_Mask );
    To copy to clipboard, switch view to plain text mode 
    That return the value 1, I guess it's normal because there is one border on top and one on bottom so that's (2*FrameWidth).
    Is it the correct styleHint or that can cause problems ?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeWidget minimum height

    Hmm... actually I think I told you wrong. You should use pixelMetric() with PM_DefaultFrameWidth or alike.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeWidget minimum height

    ok Thanks, I have changed like that :
    Qt Code:
    1. void CPropertyEditorWidget::SetFixedHeightToShowAllProperties()
    2. {
    3. // Iterator of all items.
    4. QTreeWidgetItemIterator AllIterator( m_TreeWidget, QTreeWidgetItemIterator::All );
    5.  
    6. // Initialize the SpinBox style option.
    7. const int FrameWidth = m_TreeWidget->style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
    8.  
    9. // Height of all item and header.
    10. int Height = m_TreeWidget->header()->height() + (2 * FrameWidth);
    11.  
    12. // While iterator is valid.
    13. while( *AllIterator )
    14. {
    15. // Update the height.
    16. QTreeWidgetItem* Item = *AllIterator;
    17. Height += m_TreeWidget->visualItemRect( Item ).height();
    18.  
    19. // Next iterator.
    20. ++AllIterator;
    21. }
    22.  
    23. // Set the fixed height.
    24. setFixedHeight( Height );
    25. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeWidget minimum height

    Wouldn't it be easier to query for the visual rect of the last item only? You could then take its bottom() value.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeWidget minimum height

    I have tried :
    Qt Code:
    1. void CPropertyEditorWidget::SetFixedHeightToShowAllProperties()
    2. {
    3. const int FrameWidth = m_TreeWidget->style()->pixelMetric( QStyle::PM_DefaultFrameWidth );
    4. const int LastItemBottom = m_TreeWidget->visualItemRect( m_TreeWidget->topLevelItem( m_TreeWidget->topLevelItemCount() - 1 ) ).bottom();
    5. setFixedHeight( m_TreeWidget->header()->height() + ( 2 * FrameWidth ) + LastItemBottom );
    6. }
    To copy to clipboard, switch view to plain text mode 
    But that doesn't work, I have the scrollbar on the right, using the iterator version the scrollbar is not visible.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeWidget minimum height

    Due to how bottom() is interpreted (see the docs) you should add 1 to it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  11. #11
    Join Date
    May 2013
    Posts
    321
    Thanks
    9
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTreeWidget minimum height

    Note that for historical reasons this function returns top() + height() - 1; use y() + height() to retrieve the true y-coordinate.
    I have added the +1 on the "bottom()" version and that works good, thanks for the comment on bottom(), this version is faster surely.

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTreeWidget minimum height

    Just keep in mind your code will not work for trees where items have children. You'd have to recursively descend to the deep-most visible child of the last item.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to make a QTreeWidget readjust row height!
    By Berryblue031 in forum Qt Programming
    Replies: 4
    Last Post: 29th May 2013, 18:31
  2. QTreeWidget - setting an absolute height
    By bmahf in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2011, 21:07
  3. Replies: 2
    Last Post: 18th January 2011, 05:12
  4. resize Dialog to minimum height
    By pospiech in forum Qt Programming
    Replies: 7
    Last Post: 25th June 2009, 14:04
  5. Replies: 1
    Last Post: 21st November 2008, 16:06

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.