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. Layout question
Forum Updated to NodeBB v4.3 + New Features

Layout question

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 282 Views 1 Watching
  • 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.
  • DriftwoodD Offline
    DriftwoodD Offline
    Driftwood
    wrote on last edited by
    #1

    Instead of using Qt Creator, I'm coding all the widgets in my source file. I'm watching a guy on YT do this, following along with him. I decided to venture out on my own a little and I hit a snag. I'm trying to keep a QSpinBox pressed hard to the bottom of the form. But when I grab the form and resize it, the QSpinBox floats off the bottom and rises up. How can I make the QSpinBox stick to the bottom of the form?

    #include "widget.h"
    
    #include <QHBoxLayout>
    #include <QVBoxLayout>
    #include <QLabel>
    #include <QSpacerItem>
    #include <QSpinBox>
    
    Widget::Widget(QWidget *parent)
        : QWidget(parent)
    {
        setWindowTitle("QSpinBox Example");
        resize(400,75);
    
        QSpacerItem *vSpacer = new QSpacerItem(0,0, QSizePolicy::Expanding,
                                               QSizePolicy::Expanding);
    
    
        QSpacerItem *hSpacer = new QSpacerItem(0,0, QSizePolicy::Expanding,
                                               QSizePolicy::Expanding);
    
        QLabel *lbl = new QLabel();
        lbl->setText("How many days are in one week?");
    
        QSpinBox *spinBox = new QSpinBox();
        spinBox->setMinimum(0);
        spinBox->setMaximum(10);
    
        QHBoxLayout *hBox = new QHBoxLayout();
        hBox->addItem(hSpacer);
        hBox->addWidget(lbl);
        hBox->addWidget(spinBox);
    
        QVBoxLayout *vBox = new QVBoxLayout(this);
        vBox->addItem(vSpacer);
        vBox->addLayout(hBox);
    }
    

    Can someone tell me what I've done wrong? Thanks.

    Pl45m4P 1 Reply Last reply
    0
    • DriftwoodD Driftwood

      Instead of using Qt Creator, I'm coding all the widgets in my source file. I'm watching a guy on YT do this, following along with him. I decided to venture out on my own a little and I hit a snag. I'm trying to keep a QSpinBox pressed hard to the bottom of the form. But when I grab the form and resize it, the QSpinBox floats off the bottom and rises up. How can I make the QSpinBox stick to the bottom of the form?

      #include "widget.h"
      
      #include <QHBoxLayout>
      #include <QVBoxLayout>
      #include <QLabel>
      #include <QSpacerItem>
      #include <QSpinBox>
      
      Widget::Widget(QWidget *parent)
          : QWidget(parent)
      {
          setWindowTitle("QSpinBox Example");
          resize(400,75);
      
          QSpacerItem *vSpacer = new QSpacerItem(0,0, QSizePolicy::Expanding,
                                                 QSizePolicy::Expanding);
      
      
          QSpacerItem *hSpacer = new QSpacerItem(0,0, QSizePolicy::Expanding,
                                                 QSizePolicy::Expanding);
      
          QLabel *lbl = new QLabel();
          lbl->setText("How many days are in one week?");
      
          QSpinBox *spinBox = new QSpinBox();
          spinBox->setMinimum(0);
          spinBox->setMaximum(10);
      
          QHBoxLayout *hBox = new QHBoxLayout();
          hBox->addItem(hSpacer);
          hBox->addWidget(lbl);
          hBox->addWidget(spinBox);
      
          QVBoxLayout *vBox = new QVBoxLayout(this);
          vBox->addItem(vSpacer);
          vBox->addLayout(hBox);
      }
      

      Can someone tell me what I've done wrong? Thanks.

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @Driftwood

      You need to differentiate between hSpacer and vSpacer.
      Currently they both push everything away from them, on both "axes".
      (Expanding in all directions)
      So your hSpacer in your inner layout is also pushing down, as only vSpacer should do, which results in moving your label and spinBox up as soon as there is some free space.

      Change to:

          QSpacerItem *vSpacer = new QSpacerItem(0,0, QSizePolicy::Minimum
                                                 , QSizePolicy::Expanding);
          QSpacerItem *hSpacer = new QSpacerItem(0,0, QSizePolicy::Expanding
                                                 , QSizePolicy::Minimum);
      

      Check with

          qDebug() << vSpacer->expandingDirections();
          qDebug() << hSpacer->expandingDirections();
      

      Should return

      Vertical
      Horizontal

      not

      Vertical | Horizontal
      Vertical | Horizontal

      Also:
      Better use setLayout(vbox) instead of just making one a child of your widget.
      When you have multiple layouts, it increases readability, because you dont have to look out where you pass this to make it the outermost layout.


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      DriftwoodD 1 Reply Last reply
      1
      • Pl45m4P Pl45m4

        @Driftwood

        You need to differentiate between hSpacer and vSpacer.
        Currently they both push everything away from them, on both "axes".
        (Expanding in all directions)
        So your hSpacer in your inner layout is also pushing down, as only vSpacer should do, which results in moving your label and spinBox up as soon as there is some free space.

        Change to:

            QSpacerItem *vSpacer = new QSpacerItem(0,0, QSizePolicy::Minimum
                                                   , QSizePolicy::Expanding);
            QSpacerItem *hSpacer = new QSpacerItem(0,0, QSizePolicy::Expanding
                                                   , QSizePolicy::Minimum);
        

        Check with

            qDebug() << vSpacer->expandingDirections();
            qDebug() << hSpacer->expandingDirections();
        

        Should return

        Vertical
        Horizontal

        not

        Vertical | Horizontal
        Vertical | Horizontal

        Also:
        Better use setLayout(vbox) instead of just making one a child of your widget.
        When you have multiple layouts, it increases readability, because you dont have to look out where you pass this to make it the outermost layout.

        DriftwoodD Offline
        DriftwoodD Offline
        Driftwood
        wrote on last edited by
        #3

        @Pl45m4 - Thank you for your spot-on response. I now see that the first QSizePolicy is for horizontal (width) and the second one is for the vertical (height), all of which is clearly laid out in the help files,

        QSpacerItem::QSpacerItem(int w, int h, QSizePolicy::Policy hPolicy = QSizePolicy::Minimum, QSizePolicy::Policy vPolicy = QSizePolicy::Minimum)
        

        stuff I was too blind to see last night =) Thank you for your help.

        1 Reply Last reply
        0
        • DriftwoodD Driftwood has marked this topic as solved on

        • Login

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