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.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
Phil
  • 13,875
  • 21
  • 81
  • 126

3 Answers3

18

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

gridLayout->addWidget(new QPushButton("Button"), 0, 0, Qt::AlignTop);
Oleg Shparber
  • 2,732
  • 1
  • 18
  • 19
  • 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! ;-) – Phil Dec 25 '12 at 21:20
  • It's strange because I tried `gridLayout->setContentsMargins(50, 50, 50, 50);` and it does the thing. – Oleg Shparber Dec 25 '12 at 21:25
  • 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! ;-) – Phil Dec 25 '12 at 21:53
  • In PyQt from PyQt5.QtCore import * grid.addWidget(imgLabel, 0, 0, Qt.AlignTop) – Vladimir Stazhilov May 19 '17 at 09:57
9

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.

Community
  • 1
  • 1
Synxis
  • 9,236
  • 2
  • 42
  • 64
  • Hello @Synxis! Thanks for the reply. I will keep that it mind and in my toolbox for future reference. ;-) Thanks again! – Phil Dec 25 '12 at 21:21
  • You're welcome. Don't forget you can use it not only for toolbars but also for your problem with QGridLayout! – Synxis Dec 25 '12 at 21:23
  • 1
    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. – Phil Dec 25 '12 at 21:52
1

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

jjxtra
  • 20,415
  • 16
  • 100
  • 140
  • 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. – Seth D. Fulmer Oct 21 '21 at 19:22