Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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
    Pic1
    legend:
    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->setDirection(QVBoxLayout::TopToBottom);
       inventoryLayout->setContentsMargins(0, 0, 0, 0);
       inventoryLayout->setSpacing(0);
       setLayout(inventoryLayout);
    

    after that I add widgets into the layout like this:

        for (int cnt = 0; cnt < 6; cnt++)
        {
            InventoryItemWidget *item = new InventoryItemWidget(this);
            inventoryLayout->addWidget(item);
        }    
        inventoryLayout->addStretch();
    

    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
    Pic2
    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 postimage.org, so should be fine this time
    Pic1
    Pic2
    Pic3
    Pic4



  • Oh my, seriously not even a slightest clue?


  • Moderators

    @MasterBLB

    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

    @MasterBLB
    Hi
    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.