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

QVboxLayout does not works as expected



  • Hello fellow Qt developers

    I have spotted very strange behavior of QVboxLayout. I add it to a widget like this:

    InventoryWidget::InventoryWidget(QWidget *parent)
    : InventoryBase(parent)
    {
        pen = QPen(Qt::white, penSize);
    
        inventoryLayout = new QVBoxLayout(this);
        inventoryLayout->setContentsMargins(0, 0, 0, 0);
        inventoryLayout->setSpacing(0);
    }
    

    and then fill it with items:

    void InventoryWidget::setupInventory(const QVector<const InventoryItem*> &inventory)
    {
        int offset = 0;
        for (const InventoryItem *item : inventory)
        {
            InventoryItemWidget *newItem = new InventoryItemWidget(item, this);
            inventoryLayout->addWidget(newItem);
        }
        inventoryLayout->addStretch();
    }
    

    The issue is that QSpacer added by addStretch() is moved outside the InventoryWidget into its parent, and placed between Strip Stuff button and InventoryWidget.

    Legend:
    red rectangle - widget the question is about
    green rectangle - layout which contains one or two MechPartWidgets, and spacer. You can see it in "MechDesign in Designer"
    blue rectangle - MechPartWidget, you can see how it is designed in MechPartWidget in Designer
    2_1549802891052_Pic1.png

    With layout - wrong:
    3_1549802891052_With layout.PNG

    Without layout - good:
    4_1549802891052_Without layout.PNG

    MechPartWidget:
    1_1549802891052_MechPartWidget in Designer.PNG

    MechDesignWidget:
    0_1549802891051_MechDesign in Designer.PNG


  • Lifetime Qt Champion

    Hi
    It all looks pretty fine
    Could you try to do
    qApp->setStyleSheet("QWidget { border: 1px solid red; }");

    so we can see where the various Widgets ends ?
    Im not convinced that a spacer can jump out of its Widget/layout so i think something else
    is going on.



  • @mrjj sure mate, here they are:
    1_1550003965578_Without layout - red borders.PNG 0_1550003965577_With layout - red borders.PNG
    An explanation - blue rectangle is actual border of InventoryWidget, as it has reimplemented paintEvent like:

    void InventoryWidget::paintEvent(QPaintEvent *event)
    {
        QWidget::paintEvent(event);
        QPainter painter(this);
        painter.setBrush(QBrush(Qt::darkGray));
        painter.setPen(pen);
        //painter.setRenderHint(QPainter::Antialiasing, true);
    
        painter.drawRect(penSize/2, penSize/2, slotWidth, slotsCount * slotHeight);
        for (int cnt = 1; cnt < slotsCount; cnt++)
        {
            painter.drawLine(penSize/2, cnt * slotHeight + penSize/2, slotWidth, cnt * slotHeight + penSize/2);
        }
    
        if (hasAddEquipmentDialogOn)
        {
            painter.setPen(QPen(Qt::blue, 4));
            painter.setBrush(Qt::NoBrush);
            painter.drawRect(2, 2, width() - 4, height() - 4);
        }
    }
    


  • i has some in experiences like this and i solved it by adding more QSpacer items. so you may try adding more strech.
    in my case in happened when my widget size was too big in some big monitors.



  • So many Qt experts here, yet no one can help...tough luck :/


  • Lifetime Qt Champion

    Hi,

    Might be a silly question but shouldn't you add the stretch only if you have something in the inventory vector ?



  • Hey SGaist
    If I don't add stretcher these items (M Lasers) are scattered evenly among all the space of InventoryWidget, and I'd like them to be aligned to top.
    Looks like I'll have to manually calculate and set offsets for InventoryItemWidgets...


  • Lifetime Qt Champion

    Hi
    The red border didnt show anything useful sadly.
    I think the spacer thats already in the visual UI form, is sometimes
    in wrong place and produce the space.
    But hard to debug as spacers cant be seen.
    It could also be the space above Strip stuff is sometimes not used/compressed or
    something like that.


Log in to reply