Layout question
-
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.
-
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.
You need to differentiate between
hSpacerandvSpacer.
Currently they both push everything away from them, on both "axes".
(Expandingin all directions)
So yourhSpacerin your inner layout is also pushing down, as onlyvSpacershould 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
Horizontalnot
Vertical | Horizontal
Vertical | HorizontalAlso:
Better usesetLayout(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 passthisto make it the outermost layout. -
You need to differentiate between
hSpacerandvSpacer.
Currently they both push everything away from them, on both "axes".
(Expandingin all directions)
So yourhSpacerin your inner layout is also pushing down, as onlyvSpacershould 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
Horizontalnot
Vertical | Horizontal
Vertical | HorizontalAlso:
Better usesetLayout(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 passthisto make it the outermost layout.@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.
-
D Driftwood has marked this topic as solved on