Fix the position of a layout, which is present on top of another layout, at the bottom of the screen.
-
I have my main layout as a QVBoxLayout. In that, I am adding a grid layout and a QHBoxLayout. In the QHBoxLayout, I have a next button and a previous button. When I click on next button, the size of the grid decreases (as number of elements in the grid decreases). Due to this, these buttons shift upwards. But I want these buttons to not move in the vertical direction. They should have a fixed position. How to do this, without using a spacer?
-
I have my main layout as a QVBoxLayout. In that, I am adding a grid layout and a QHBoxLayout. In the QHBoxLayout, I have a next button and a previous button. When I click on next button, the size of the grid decreases (as number of elements in the grid decreases). Due to this, these buttons shift upwards. But I want these buttons to not move in the vertical direction. They should have a fixed position. How to do this, without using a spacer?
@shreya_agrawal
If I understand right, on aQVBoxLayout
you have aQGridLayout
above aQHBoxLayout
. If you want theQHBoxLayout
to stay at the bottom of theQVBoxLayout
regardless of what is above it (QGridLayout
) then you are indeed supposed to use verticalQSpacerItem
above it to "push" it to the bottom. So not sure why you don't what that or what the alternative would be. -
@shreya_agrawal
If I understand right, on aQVBoxLayout
you have aQGridLayout
above aQHBoxLayout
. If you want theQHBoxLayout
to stay at the bottom of theQVBoxLayout
regardless of what is above it (QGridLayout
) then you are indeed supposed to use verticalQSpacerItem
above it to "push" it to the bottom. So not sure why you don't what that or what the alternative would be.@JonB
Thank you for your reply !
I actually implemented the QSpacerItem, but I have been told that it could also be done in another way, by using some property of QVBoxLayout. So, I just wanted to confirm if any such property exists. -
@JonB
Thank you for your reply !
I actually implemented the QSpacerItem, but I have been told that it could also be done in another way, by using some property of QVBoxLayout. So, I just wanted to confirm if any such property exists.@shreya_agrawal
I find layout-ing to be a dark art!Most likely you are thinking of: when adding your widget(s) to the layout have a look at void QBoxLayout::addWidget(QWidget *widget, int stretch = 0, Qt::Alignment alignment = Qt::Alignment()). The optional stretch parameter allows you pass, say,
2
for one widget and1
for another. Then it would allocate two-thirds of the available space to the first one and one-third to the second. Which won't do you. You could try passing1
for the top one and0
for the bottom one, but I don't think that allocates all space for the former and minimal for the latter, though you could test. And/or you could try passingQt::AlignBottom
for alignment on the bottom one, again not sure if that does what you want. Try them. (I believe you can set necessary properties for these from Qt Designer if not doing it in code.) If they work you have penetrated a portion of the dark art :)My recollection is being told that for your case putting in
QSpacerItem
s is the way to do it. -
@shreya_agrawal
I find layout-ing to be a dark art!Most likely you are thinking of: when adding your widget(s) to the layout have a look at void QBoxLayout::addWidget(QWidget *widget, int stretch = 0, Qt::Alignment alignment = Qt::Alignment()). The optional stretch parameter allows you pass, say,
2
for one widget and1
for another. Then it would allocate two-thirds of the available space to the first one and one-third to the second. Which won't do you. You could try passing1
for the top one and0
for the bottom one, but I don't think that allocates all space for the former and minimal for the latter, though you could test. And/or you could try passingQt::AlignBottom
for alignment on the bottom one, again not sure if that does what you want. Try them. (I believe you can set necessary properties for these from Qt Designer if not doing it in code.) If they work you have penetrated a portion of the dark art :)My recollection is being told that for your case putting in
QSpacerItem
s is the way to do it.@JonB
Thank you for your time !
I tried using the alternative solutions you mentioned, but none of them is giving the desired results. I guess I will stick to using a QSpacerItem. -