Results 1 to 10 of 10

Thread: Get sizeHint of a QLayout, doesn't update after adding items

  1. #1
    Join Date
    Aug 2010
    Location
    Germany
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Angry Get sizeHint of a QLayout, doesn't update after adding items

    It appears that QLayout doesn't update sizeHint() any time a new item is added. Instead it seems like it differs it to some future time. I realize this by adding some items, checking sizeHint of the layout and seeing that it doesn't change.

    I need a way to get the sizeHint immediately after adding several items to the layout. This is part of a system where a user selects several widgets and then the new window size is the preferred size of the layout. The user can however adjust the size later so I can't just forcibly use the size policy system.

    Any ideas on how to get the proper sizeHint from QLayout immediately after adding several items? Calling "update" on it doesn't appear to do it.

  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: Get sizeHint of a QLayout, doesn't update after adding items

    Try calling QLayout::activate().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2010
    Location
    Germany
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Get sizeHint of a QLayout, doesn't update after adding items

    I've tried that and it doesn't change the situation.

    I've also checked now that as I add the elements the sizeHint on all the individual elements appears to be correct.


    Added after 14 minutes:


    Tracking through the QT source the best I can say is that it is a defect in the QWidgetItemV2 class. The underlying widget has the correct sizeHint, but this LayoutItem wrapper is reporting 0,0 for max/min/hint sizes.

    Now I have to look for a workaround.
    Last edited by mortoray; 3rd December 2010 at 09:38.

  4. #4
    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: Get sizeHint of a QLayout, doesn't update after adding items

    Does it return true or false?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Aug 2010
    Location
    Germany
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Confirmed QT Defect

    Didn't check. Wouldn't matter. If it did return "false" I'd still be stuck in the same position: not knowing how to get the size.


    Added after 6 minutes:


    This simple code shows that sizeHint doesn't work on the items if the target widget is already visible. I'll go file this at nokia.

    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main( int argc, char ** argv )
    4. {
    5. QApplication app( argc, argv );
    6.  
    7. QWidget * win = new QWidget();
    8. win->show(); //without this here it works as expected
    9.  
    10. QBoxLayout * layout = new QHBoxLayout( win );
    11. QPushButton * button = new QPushButton( win );
    12. button->setText( "Sample" );
    13. layout->addWidget( button );
    14.  
    15. qDebug() << button->sizeHint() << layout->itemAt( 0 )->sizeHint() << layout->sizeHint();
    16. }
    To copy to clipboard, switch view to plain text mode 


    Added after 15 minutes:


    QT Defect:
    http://bugreports.qt.nokia.com/browse/QTBUG-15832
    Last edited by mortoray; 3rd December 2010 at 10:09.

  6. #6
    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: Confirmed QT Defect

    Quote Originally Posted by mortoray View Post
    Didn't check. Wouldn't matter. If it did return "false" I'd still be stuck in the same position: not knowing how to get the size.
    It does matter.

    This simple code shows that sizeHint doesn't work on the items if the target widget is already visible. I'll go file this at nokia.
    It's not a bug, the behaviour is correct. Your report will be rejected.

    The real "bug" is in your way of thinking because you "think" you need the value immediately whereas in reality you don't. You only need it when the layout repositions the items it controls which is much later than "immediately". Stop thinking synchronously, start thinking asynchronously - it's not easy but it's worth it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Aug 2010
    Location
    Germany
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Confirmed QT Defect

    It's not a bug, the behaviour is correct. Your report will be rejected.
    I have provided the exact reason why I need this value immediately. I wish to automatically size my parent widget when the user changes the items that are present in it.

    Because I have a workaround there is no reason not to believe this isn't just a defect in the QT code.

  8. #8
    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: Confirmed QT Defect

    Quote Originally Posted by mortoray View Post
    I have provided the exact reason why I need this value immediately.
    Well, I can say that I need 2+2 to be 5 but it doesn't make it a bug that 2+2 is really 4.

    I wish to automatically size my parent widget when the user changes the items that are present in it.
    This does not require immediate knowledge of the layout size. You only need that when the Qt decides (by processing the events from the event loop) to show or hide those items which is waaaay later than when you put those items in the layout.

    Because I have a workaround there is no reason not to believe this isn't just a defect in the QT code.
    I can have a workaround for the 2+2=4 "issue" which doesn't make it a defect in C++ specification.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Aug 2010
    Location
    Germany
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Confirmed QT Defect

    Quote Originally Posted by wysota View Post
    This does not require immediate knowledge of the layout size. You only need that when the Qt decides (by processing the events from the event loop) to show or hide those items which is waaaay later than when you put those items in the layout.
    Please provide a solution on how to do as I wish without requiring the size immediately. After the user changes the items in a display (Widgets in a BoxLayout) the parent widget should be resized to the preferred size of the layout (sizeHint under normal circumstances). The user may, after this point, adjust the size as they see fit.

  10. #10
    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: Confirmed QT Defect

    Quote Originally Posted by mortoray View Post
    Please provide a solution on how to do as I wish without requiring the size immediately.
    I'm here to help you find solutions not to provide ready solutions for you. In extremly interesting cases I do that but that's certainly not one of them.

    To help you understand why what you are trying to do doesn't work and shouldn't work let's make a simple theoretical experiment. Suppose you have a widget containing a bunch of children that sums to a total sizeHint() of 200x200. Now you add another widget (with sizeHint of 200x100) to a vertical layout controling the main widget. But the new child remains hidden - should the sizeHint of the layout immediately become 200x300?

    After the user changes the items in a display (Widgets in a BoxLayout) the parent widget should be resized to the preferred size of the layout (sizeHint under normal circumstances). The user may, after this point, adjust the size as they see fit.
    So you always want to force the sizeHint() regardless of the current size of the widget, yes? Hmm... strange but ok. There are a couple of ways to do it or at least a couple of things to look into. The first thing I would try would be to set the size constraint (Minimum) on the layout. If that's not what you want then you can for example subclass the layout class to provide a signal you can connect to whenever the layout is recalculated (this is most probably done in QLayoutItem::setGeometry() but you have to check it out in the source code). Then connect to the signal and resize the main widget. Instead of emitting a signal you can also resize the widget directly from within the layout effectively simulating what size constraints do.

    You may also get away with simply reimplementing resizeEvent() of the main widget.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 0
    Last Post: 20th September 2010, 09:58
  2. Replies: 1
    Last Post: 19th April 2010, 11:27
  3. How to update items in a QTreeView
    By laugusti in forum Qt Programming
    Replies: 5
    Last Post: 12th August 2009, 08:03
  4. update items in QTableWidget
    By darshan in forum Qt Programming
    Replies: 13
    Last Post: 3rd March 2009, 22:43
  5. QLayout update size signal
    By bunjee in forum Qt Programming
    Replies: 3
    Last Post: 6th June 2008, 11:24

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.