Results 1 to 8 of 8

Thread: QHeaderView section resizes

  1. #1
    Join Date
    Aug 2007
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QHeaderView section resizes

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHeaderView section resizes

    Take a look at QHeaderView::setResizeMode.

  3. #3
    Join Date
    Aug 2007
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHeaderView section resizes

    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.

  4. #4
    Join Date
    Aug 2007
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHeaderView section resizes

    I.e. I need something like setStretchLastSection(), but not for the last one.

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHeaderView section resizes

    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.

  6. #6
    Join Date
    Aug 2007
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHeaderView section resizes

    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:

    setResizeMode(ResizeToContents);
    addItem()
    setResizeMode(Interactive);

    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!

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QHeaderView section resizes

    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).

  8. #8
    Join Date
    Aug 2007
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QHeaderView section resizes

    Thanks for ideas!
    I'll surely look more deeply into this tomorrow
    Running away from work atm

Similar Threads

  1. Highlighting selected section on QDateTimeEdit
    By Yorma in forum Qt Programming
    Replies: 4
    Last Post: 3rd May 2013, 12:03
  2. Algorithms problem of brackets including.
    By luffy27 in forum General Programming
    Replies: 2
    Last Post: 12th April 2007, 01:10

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.