Strange behavior of widgets in QVBoxLayout

  • Hey Qt devs

    I've encountered a strange issue - though I've added a vertical QSpacerItem the space is not fully consumed by it.
    1st, a picture:
    alt text
    red rectangle - widget the question is about
    green rectangle - parent QVboxLayout which contains widget(s) with the above layout, and a spacer item
    blue elements - QSpacerItem

    I've adde to the widget illustrated by these grey tables with white borders own QVBoxLayout:

       inventoryLayout = new QVBoxLayout;
       inventoryLayout->setContentsMargins(0, 0, 0, 0);

    after that I add widgets into the layout like this:

        for (int cnt = 0; cnt < 6; cnt++)
            InventoryItemWidget *item = new InventoryItemWidget(this);

    this seems to work correctly, these InventoryItemWidgets are pushed towards top edge of InventoryWidget.
    Strange things start to happen when I try to stretch the window vertically - it looks like the spacer created by inventoryLayout->addStretch() went from the red layout into the green layout, and places between "Strip stuff" button and InventoryWidget. Here is screenshot how it looks like:
    alt text
    If I will remove that line the widgets behave correctly, though these M Laser objects are of course not aligned to the top edge of inventory.
    What detail about layouts/widgets I'm missing?

    Definitions in the .ui files:
    alt text
    alt text

  • Hmm no one have a clue?

  • Qt Champions 2017

    Can you share your images & workable example somewhere ? Images seems to be missing.

  • Strange, I can see them o.O

  • Qt Champions 2017

    At least I am not able see the images. If u can share it and also workable code to check it will help us to help you.

  • Hmm, edit feature works strane, so I'll add the pictures here. Uploaded to, so should be fine this time

  • Oh my, seriously not even a slightest clue?

  • Moderators


    soryy can't help here much,

    But let me ask, how are you managing the position of your 5 vertical layouts ?

    Are they also managed by a QLayout ? QHBoxLayout seems appropriate.

  • Lifetime Qt Champion

    I read it a few times but it could be anything.
    My first guess was that you insert the "Right arm" "HEAD" etc after the spacer.
    Like its already in UI file and you just use layout->addwidget so it comes after spacer.

  • It is as follows:
    Pic 3 with these long spacers contains complex widgets MechPartWidget presented in pic 4. You can see a tiny selection above the 2nd vertical spacer
    Then, in MechPartWidget there are no layouts/widgets with vertical horizontal policy set to expanding.
    Next, in the MechPartWidget there is a custom widget InventoryWidget. I tried to add QVBoxLayout to it, several widgets, and then a vertical spacer - and I expect this own-added layout would be honoured only inside boundaries of InventoryWidget, yet it isn't.

Log in to reply