Results 1 to 6 of 6

Thread: widgets aren't resized correctly when adding them to layout

  1. #1
    Join Date
    Feb 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default widgets aren't resized correctly when adding them to layout

    hello!
    i have the following structure:
    QWidget->QWidget->QBoxLayout->[QWidget1...QWidgetN]
    where QWidget1 to QWidgetN are inserted one after another while the top-widgets are visible.
    the problem is that while the widgets are added, the layout doesn't resize correctly, such that the widgets are way too small and their content is not visible.
    the best way to see what i mean is by compiling the following (short) example:
    http://homepages.physik.uni-muenchen...zeTestNew2.zip
    the problem doesn't occur for widgets that are added before the main widget is visible... also, in the example QWidget1...QWidgetN are QWidgets containing a QHBoxLayout with a QLayout and a QDoubleSpinBox. When using JUST a QLabel, the problem doesn't occur...
    how can i make the layout resize correctly to its contents? calling resize(sizeHint()) doesn't help, neither does calling updateGeometry and adjustSizes...

    thanks,

    thomas

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: widgets aren't resized correctly when adding them to layout

    "container-widget" is a child of "top-widget" but not in a layout.

    Set a layout on "top-widget" inside the scroll area, add "container-widget", and dispense with the attempts to force geometry in the addNewWidget() method. See if this helps.

  3. #3
    Join Date
    Feb 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: widgets aren't resized correctly when adding them to layout

    unfortunately this is not a possibility since in top-widget there will be several container-widgets that are freely movable by the user...
    but what would be the difference? in other words: what methods would a layout call on container-widget that makes it update the geometry correctly other than updateGeometry()?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: widgets aren't resized correctly when adding them to layout

    The layout inside your "container-widget" attempts to layout the specified children within the space it has available. It will not grow the container (which is what a layout of "top-widget" would do). The parent widget layout will (presumably) call setGeometry() and move() on its child widgets once it has worked out where to put them and what size they should be.

    How do you intend the container widgets to be sized?

  5. #5
    Join Date
    Feb 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: widgets aren't resized correctly when adding them to layout

    I see. The thing is: if the widgets that are added in addNewWidget() (let's call them items) are simply QLabel instead of the more complex widgets (i.e. having a layout and children in that layout), the container gets resized correctly, so i think (also according to your explanation) that the layouts of the items don't try to resize the item, such that their children don't occupy the space i would expect them to.
    what i would expect:
    once an item is added to the container widget, the latter grows such that all children occupy their preferred space (i.e. according to their sizeHint()?).
    so thinking about it i guess my problem is not the container widget not growing to satisfy its children's sizeHints but rather the sizeHint of the items not being correct...
    would it help to adjust the sizePolicy of all widgets inside an item (the QLabel and the QDoubleSpinBox in my example) to be minimum of minimumExpanding?

  6. #6
    Join Date
    Feb 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: widgets aren't resized correctly when adding them to layout

    ha! i got it!
    ok, first of all sorry for replying to my own post, i've been trying around as i was writing here on the forum...
    setting the sizePolicy didn't do the trick, but setting the sizeConstraint to QLayout::SetMinimumSize on all layouts does exactly what i want. by all layouts i mean really all layouts: the layout of the container-widget, the layout of the groupBox inside the latter layout and the layout of the items.
    maybe this can help someone who has the same problem in the future.
    thanks for sticking around, ChrisW67!

Similar Threads

  1. Embedded ARM Seg Faults Adding Widget to Layout
    By flob in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 22nd December 2009, 23:22
  2. Adding more tab widgets
    By rakkar in forum Newbie
    Replies: 2
    Last Post: 10th September 2009, 21:22
  3. Adding widgets dynamically
    By miw in forum Qt Programming
    Replies: 1
    Last Post: 23rd July 2009, 10:31
  4. A question about adding to layout
    By Cruz in forum Qt Programming
    Replies: 1
    Last Post: 20th January 2009, 12:41
  5. Adding qpaint object to a Layout
    By Rooster in forum Newbie
    Replies: 6
    Last Post: 13th February 2008, 13:12

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.