PDA

View Full Version : How to make a QTreeWidget readjust row height!



Berryblue031
10th June 2011, 09:26
I have a QTreeWidget, that uses a custom item delegate to the paint it's items. These items can contain wrapping text.

When I resize the widget the text wraps differently changing the height requirements, but I can't get the row height to update on resize! if I hide and show the widget the height is properly adjusted.

What can I do in the resize event of the QTreeWidget to get it to recalculate the height of it's rows?

joyer83
10th June 2011, 10:06
Have you tried to emit this signal?


void QAbstractItemDelegate::sizeHintChanged ( const QModelIndex & index ) [signal]

This signal must be emitted when the sizeHint() of index changed.
Views automatically connect to this signal and relayout items as necessary.
This function was introduced in Qt 4.4.

MarekR22
10th June 2011, 10:49
the thing is a bit complicated. I did similar thing with custom Label is some project.
sizeHint have to be calculated in two phases.
In first phase you don't now anything about geometry so you return sizeHint like text would not be wrapped.
Then when you size of that item is set you are checking if wrapping is needed.
If it is then you emit signal sizeHintChanged! Now in sizeHint you have to return size with width like in unwrapped text (important) and new height for wrapped text.
For performance reasons you have to cache those values (text mesurements are quite heavy and you have to prevent repetitive calls of sizeHintChanged to prevent infinitive loop).

andrap
16th July 2011, 17:06
In first phase you don't now anything about geometry so you return sizeHint like text would not be wrapped.
Then when you size of that item is set you are checking if wrapping is needed.
If it is then you emit signal sizeHintChanged! Now in sizeHint you have to return size with width like in unwrapped text (important) and new height for wrapped text.

Sounds good, but the question is "when can a sizeHintChanged be emitted?"
All the virtual methods that one might override are const methods. No signals can be emitted from const methods. So it looks like a dead end...

christopher.j.meacham
29th May 2013, 18:31
Super old thread, I know, but thank you guys for the solution to a problem I've been researching for weeks. I couldn't get my QTreeView to adjust row height, no matter what I did, and the current Qt 4.7 documentation does not display inherited methods, so I had no idea sizeHintChanged() even existed. I already had a QItemDelegate in order to change the icon to the indexed image in a QFileSystemModel, so I just added a method to emit sizeHintChanged() whenever I change the icon size. It's very nice.

Thanks again!