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.
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]
This does not regard items outside of the visual field.Originally Posted by jacek
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:
Full code:Qt Code:
// must give me the size of the text, this is just a workaround (char number * 7): treeWidget_.topLevelItem( i )->text( col ).size()*7To copy to clipboard, switch view to plain text mode
Qt Code:
{ 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 ); } }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]
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.
FaS (2nd July 2006)
You can get QFontMetrics without QPainter --- just use QWidget::fontMetrics().Originally Posted by rickbsgu
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]
I guess it will be fixed in Qt 4.2: http://www.trolltech.com/developer/t...entry&id=88644Originally Posted by FaS
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]
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.Originally Posted by FaS
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]
Bookmarks