Results 1 to 10 of 10

Thread: Resize QTreeWidget columns to contents

  1. #1
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Resize QTreeWidget columns to contents

    Hello there!

    As said int the title, I simply want to resize the column widths in a QTreeWidget to fit to the contents.
    Just like windows' listview controls behave when double clicking between their header items.

    Hope anybody can help me.
    I'm german, please excuse my english.
    nevees.de.vu - nicht eingetragener Verein zur Entwicklung experimenteller Spiele
    unregistered club for developing experimental games [german]

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resize QTreeWidget columns to contents


  3. #3
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: Resize QTreeWidget columns to contents

    Quote Originally Posted by jacek
    This does not regard items outside of the visual field.

    I also could think about enumerating all top level items in each column and calculate the optimal size (width) manually, but how??
    The following code works except that the width (in pixels) the item text occupies is not calculated properly. I just don't know how to do that:
    Qt Code:
    1. // must give me the size of the text, this is just a workaround (char number * 7):
    2. treeWidget_.topLevelItem( i )->text( col ).size()*7
    To copy to clipboard, switch view to plain text mode 
    Full code:
    Qt Code:
    1. void resizeColumnsToContents( QTreeWidget &treeWidget_ )
    2. {
    3. int cCols = treeWidget_.columnCount();
    4. int cItems = treeWidget_.topLevelItemCount();
    5. int w;
    6. int col;
    7. int i;
    8. for( col = 0; col < cCols; col++ ) {
    9. w = treeWidget_.header()->sectionSizeHint( col );
    10. for( i = 0; i < cItems; i++ )
    11. w = qMax( w, treeWidget_.topLevelItem( i )->text( col ).size()*7 + (col == 0 ? treeWidget_.indentation() : 0) );
    12. treeWidget_.header()->resizeSection( col, w );
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    I'm german, please excuse my english.
    nevees.de.vu - nicht eingetragener Verein zur Entwicklung experimenteller Spiele
    unregistered club for developing experimental games [german]

  4. #4
    Join Date
    Jan 2006
    Location
    Earth (Terra)
    Posts
    87
    Thanks
    4
    Thanked 6 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Resize QTreeWidget columns to contents

    Without digging up the actual calls, to get the size of text, you basically need to do something like this:

    • Get or create a QPaint context for the widget.
    • Get the QFontMetrics from the paint context.
    • Use the BoundingRect function in the QFontMetrics to get the size of the passed-in string.

  5. The following user says thank you to rickbsgu for this useful post:

    FaS (2nd July 2006)

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resize QTreeWidget columns to contents

    Quote Originally Posted by rickbsgu
    • Get or create a QPaint context for the widget.
    • Get the QFontMetrics from the paint context.
    You can get QFontMetrics without QPainter --- just use QWidget::fontMetrics().

  7. #6
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Resize QTreeWidget columns to contents

    Thanks a lot rickbsgu and jacek!!

    Because an item in a QTreeWidget needs more space than only the text width (~6px), I've added some pixels there ignoring the used theme. It would nice to retrieve this size at runtime, but I could live with the current solution.

    I'm just wondering why it's so complicated to imitate the behavior of standard windows list view controls. Why QTreeView::resizeColumnToContents() only regards the seeable items; considering all present items would have been more easy to program or not? Who in god's name double clicks between the column headers everytime he scrolls down a list, only to see the full text? Nobody!! I can't understand it....
    I'm german, please excuse my english.
    nevees.de.vu - nicht eingetragener Verein zur Entwicklung experimenteller Spiele
    unregistered club for developing experimental games [german]

  8. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resize QTreeWidget columns to contents

    Quote Originally Posted by FaS
    Why QTreeView::resizeColumnToContents() only regards the seeable items; considering all present items would have been more easy to program or not?
    I guess it will be fixed in Qt 4.2: http://www.trolltech.com/developer/t...entry&id=88644

  9. The following 2 users say thank you to jacek for this useful post:

    blukske (3rd July 2006), FaS (2nd July 2006)

  10. #8
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resize QTreeWidget columns to contents

    ok now i understand the world.
    p.s.: what is the "thanks" button for? does it increase any counters like user ratings or something like that? or is it for those who are too lazy to write the text out.
    I'm german, please excuse my english.
    nevees.de.vu - nicht eingetragener Verein zur Entwicklung experimenteller Spiele
    unregistered club for developing experimental games [german]

  11. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 975 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resize QTreeWidget columns to contents

    Quote Originally Posted by FaS
    what is the "thanks" button for? does it increase any counters like user ratings or something like that? or is it for those who are too lazy to write the text out.
    It's for those who don't want to interrupt the on-topic discussion just to say "thanks", but it also increases "thanks" and "thanked" counters.

  12. #10
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    5
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Resize QTreeWidget columns to contents

    oh i don't saw these fields besides the posts.. stupid question
    I'm german, please excuse my english.
    nevees.de.vu - nicht eingetragener Verein zur Entwicklung experimenteller Spiele
    unregistered club for developing experimental games [german]

Similar Threads

  1. How to capture resizing of QTreeWidget columns?
    By simk in forum Qt Programming
    Replies: 2
    Last Post: 27th April 2006, 07:10
  2. postponing resize event
    By Honestmath in forum Qt Programming
    Replies: 11
    Last Post: 26th February 2006, 01:32

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.