Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QVboxLayout does not works as expected
QtWS25 Last Chance

QVboxLayout does not works as expected

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 979 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • MasterBLBM Offline
    MasterBLBM Offline
    MasterBLB
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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.

      1 Reply Last reply
      1
      • MasterBLBM Offline
        MasterBLBM Offline
        MasterBLB
        wrote on last edited by
        #3

        @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);
            }
        }
        
        1 Reply Last reply
        0
        • M Offline
          M Offline
          mahd96
          wrote on last edited by
          #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.

          1 Reply Last reply
          0
          • MasterBLBM Offline
            MasterBLBM Offline
            MasterBLB
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

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

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • MasterBLBM Offline
                MasterBLBM Offline
                MasterBLB
                wrote on last edited by
                #7

                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...

                1 Reply Last reply
                0
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  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.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved