Qt QGridLayout automatically centers (moves) items to the middle.
Asked Answered
A

3

12

I have a QHBoxLayout on my form with 3 layouts added to it. The second one has the items concerning my question.

I have a QVBoxLayout on the second pane of the main layout (the QHBoxLayout). This QVBoxLayout has a QScrollArea added to it with addWidget.

This QScrollArea is parent to a QWidget called "scrollContents" which in turn contains a QGridLayout.

I am adding a custom created widget to this QGridLayout which has a fixed height. We can consider this 100px for now.

If this QGridLayout has items, of which the total height is less than the form itself, it centers these widgets vertically with same amount of space between them.

If there is one single widget, it appears right in the middle. But I would like them to be listed from top to bottom.

Ex.: [### represents the area of QScrollArea in which there's a QWidget with the QGridLayout.

   OK      ->   DESIRED    -> NOT DESIRED AND WHAT HAPPENS
##########    ##########               ##########
# |item| #    # |item| #               #        #
# |item| #    #        #               #        #
# |item| #    #        #               #        #
# |item| #    #        #               # |item| #
# |item| #    #        #               #        #
# |item| #    #        #               #        #
# |item| #    #        #               #        #
##########    ##########               ##########
  |item|
  |item| 
  |item|

Basically: If there's space for 9 "rows", when a single one item is added it appears in the middle at the location of 5th. If there is 9 or more, they appear as they should. If there is 8 or less, their space in-between is expanded to center them all.

How can I solve this?

Thank you.

Aficionado answered 25/12, 2012 at 20:48 Comment(0)
S
10

There is also a method different from what trollixx answered: add a dummy widget with its vertical size policy set to expanding, at the 'bottom' of the QGridLayout. See this answer, which also contains an example with a toolbar.

Slocum answered 25/12, 2012 at 21:18 Comment(3)
Hello @Synxis! Thanks for the reply. I will keep that it mind and in my toolbox for future reference. ;-) Thanks again!Aficionado
You're welcome. Don't forget you can use it not only for toolbars but also for your problem with QGridLayout!Slocum
Hello again @Synxis. I've done various tests and read some of documentation of Qt. I believe this method you've suggested is much better of a solution. Because: it works if there's not many items added. It works if items are removed. It works if items are re-added. Therefore, I've chosen your answer is the answer. I hope it will helps others in future. If you don't mind me saying, it would be kind of cool perhaps if you edit your message to explain it bit more with perhaps code references to what's written in the linked question. Thanks again.Aficionado
R
18

QGridLayout::addWidget() has alignment parameter. The following code works for me:

gridLayout->addWidget(new QPushButton("Button"), 0, 0, Qt::AlignTop);
Rochette answered 25/12, 2012 at 21:11 Comment(4)
it worked like a charm! The only problem is, it does not respect the setContentMargins() parameters. I will look for it but if you also have experience with it, I would very much appreciate it, a lot! ;-)Aficionado
It's strange because I tried gridLayout->setContentsMargins(50, 50, 50, 50); and it does the thing.Rochette
you are right. I have some limited software texting functions which automatically adds, deleted, readds random amount of widgets and I did some coding error there. So you are actually absolutely right. This code does what it should but it lacks when there's more adding and removing. So if you don't mind, I am chosing the other post as the answer as it's a bit more of a solid solution in my case. However, I will remember yours and use it whenever necessary so I thank you sincerely! Cheers! ;-)Aficionado
In PyQt from PyQt5.QtCore import * grid.addWidget(imgLabel, 0, 0, Qt.AlignTop)Chlorinate
S
10

There is also a method different from what trollixx answered: add a dummy widget with its vertical size policy set to expanding, at the 'bottom' of the QGridLayout. See this answer, which also contains an example with a toolbar.

Slocum answered 25/12, 2012 at 21:18 Comment(3)
Hello @Synxis! Thanks for the reply. I will keep that it mind and in my toolbox for future reference. ;-) Thanks again!Aficionado
You're welcome. Don't forget you can use it not only for toolbars but also for your problem with QGridLayout!Slocum
Hello again @Synxis. I've done various tests and read some of documentation of Qt. I believe this method you've suggested is much better of a solution. Because: it works if there's not many items added. It works if items are removed. It works if items are re-added. Therefore, I've chosen your answer is the answer. I hope it will helps others in future. If you don't mind me saying, it would be kind of cool perhaps if you edit your message to explain it bit more with perhaps code references to what's written in the linked question. Thanks again.Aficionado
C
1

Use a vertical spacer at the bottom, that's what they are for.

Clanton answered 17/7, 2018 at 16:48 Comment(1)
I'm interested in your solution but what is a spacer in GridLayout? There's no such method like addStretch() like there is in QHBoxLayout and QVBoxLayout.Suazo

© 2022 - 2024 — McMap. All rights reserved.