Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Unsolved QVboxLayout does not works as expected

    General and Desktop
    4
    8
    519
    Loading More Posts
    • 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.
    • MasterBLB
      MasterBLB last edited by

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by mrjj

        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 Reply Quote 1
        • MasterBLB
          MasterBLB last edited by

          @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 Reply Quote 0
          • M
            mahd96 last edited by

            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 Reply Quote 0
            • MasterBLB
              MasterBLB last edited by

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

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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 Reply Quote 0
                • MasterBLB
                  MasterBLB last edited by

                  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 Reply Quote 0
                  • mrjj
                    mrjj Lifetime Qt Champion last edited by mrjj

                    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 Reply Quote 0
                    • First post
                      Last post