View Full Version : QHeaderView section resizes

11th October 2007, 12:24
Hello, guys!

I need to do a simple task, but i'm somehow lost about how to do it :)
Need a little help.

What I need is the following:
I have a QTreeView with 3 columns. And I want to set its header so the:
1st column has predefined size (calculated once by me)
2nd column is stretches to fit all available size
3rd column has predefined size as well.

I.e. in <b>constructor</b> I want to set two sizes that I calculate and have the middle field to be resized so it stretches to all available size.
And I want all columns to stay resizable by user, so setResizeMode( ResizeToContents ) or whatever is not an option.

How to do that? Tried many ways with no luck. I'm lost :)

11th October 2007, 13:01
Take a look at QHeaderView::setResizeMode.

11th October 2007, 13:04
Specificaly for this case I wrote that it won't do what I want :)
Please, re-read my post. I looked at it. I need all columns to stay in QHeaderView::Interactive mode.

11th October 2007, 13:06
I.e. I need something like setStretchLastSection(), but not for the last one.

11th October 2007, 13:38
setStretchLastSection does nothing more but set the resizeMode to Stretch for the last column, so it's not resizable by the user anymore. I understand that you want to provide default sizes but let the user change them afterwards, is that correct? So why can't you just use resizeSection() to resize each section to the values you calculated? I must add that doing what you want in the constructor will be difficult because at that point no geometries are set yet, so you might have to delay the calculation until the header is shown for the first time.

Sorry for misreading your post, I'm not feeling well today.

11th October 2007, 13:49
Thanks for following me once more :) And I wish you to get well!

You got me exactly right now.
The thing is that i need to somehow calculate the middle column size and it turns out (as you said) that i can't do that in constructor, because the whole header size is not yet valid.

I think i have found the solution though.
Ah, forgot to mention that my view gets constantly populated by items during some time period. And they may have different length text in each column. So ReziseToContents mode sounds ideal except that it locks headers movement.

I think I will go the following way:


This way the view will do adjustments as every item is added and immediately set the mode back (because I want the user to be able to resize during populating process).
I must test how this looks from perfomance point of view, but I guess it should be ok.

Thanks again and sorry for maybe long post!

11th October 2007, 17:40
I don't think it is a good way to go. It will be very slow as after each addition the header will have to go through all the items to calculate the needed size. It's better to cache the result from the previous calculation and only compare the last added item size to the previous largest item and set the section size to the bigger of the two. Of course you have to consider the fact that if the section was resized by the user, you shouldn't adjust it after adding an item (which will not happen if you switch to ResizeToContents and then back).

11th October 2007, 17:44
Thanks for ideas!
I'll surely look more deeply into this tomorrow :)
Running away from work atm :D