Results 1 to 5 of 5

Thread: How to make a QTreeWidget readjust row height!

  1. #1
    Join Date
    Mar 2011
    Location
    Denmark
    Posts
    74
    Thanks
    7
    Thanked 10 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default How to make a QTreeWidget readjust row height!

    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?

  2. #2
    Join Date
    Aug 2008
    Posts
    45
    Thanks
    1
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to make a QTreeWidget readjust row height!

    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.


  3. #3
    Join Date
    Nov 2010
    Posts
    315
    Thanked 53 Times in 51 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to make a QTreeWidget readjust row height!

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

  4. #4
    Join Date
    Jul 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to make a QTreeWidget readjust row height!

    Quote Originally Posted by MarekR22 View Post
    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...

  5. #5
    Join Date
    Apr 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to make a QTreeWidget readjust row height!

    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!

Similar Threads

  1. Replies: 1
    Last Post: 24th March 2011, 10:31
  2. Replies: 2
    Last Post: 18th January 2011, 05:12
  3. Replies: 1
    Last Post: 24th November 2010, 23:32
  4. Replies: 3
    Last Post: 12th August 2010, 07:20
  5. Replies: 1
    Last Post: 21st November 2008, 16:06

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
  •  
Qt is a trademark of The Qt Company.