PDA

View Full Version : Resize QTreeWidget columns to contents



FaS
1st July 2006, 14:17
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.

jacek
1st July 2006, 14:44
QTreeView::resizeColumnToContents()

FaS
2nd July 2006, 15:02
QTreeView::resizeColumnToContents()
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:


// must give me the size of the text, this is just a workaround (char number * 7):
treeWidget_.topLevelItem( i )->text( col ).size()*7

Full code:


void resizeColumnsToContents( QTreeWidget &treeWidget_ )
{
int cCols = treeWidget_.columnCount();
int cItems = treeWidget_.topLevelItemCount();
int w;
int col;
int i;
for( col = 0; col < cCols; col++ ) {
w = treeWidget_.header()->sectionSizeHint( col );
for( i = 0; i < cItems; i++ )
w = qMax( w, treeWidget_.topLevelItem( i )->text( col ).size()*7 + (col == 0 ? treeWidget_.indentation() : 0) );
treeWidget_.header()->resizeSection( col, w );
}
}

rickbsgu
2nd July 2006, 15:41
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.

jacek
2nd July 2006, 16:03
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().

FaS
2nd July 2006, 17:47
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....

jacek
2nd July 2006, 19:08
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/task-tracker/index_html?method=entry&id=88644

FaS
2nd July 2006, 19:34
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.

jacek
2nd July 2006, 20:11
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.

FaS
2nd July 2006, 21:48
oh i don't saw these fields besides the posts.. stupid question