How to align a GroupBox at the top of a VerticalLayout
-
Hi,
I am currently working on a PyQt5 application with Python 3.9.0.
Screenshot
I am trying to align a GroupBox to the top of a VerticalLayout in a HorizontalLayout.
The HorizontalLayout contains a VerticalLayout (left side, Title 2 and Title 3) and a GroupBox (right side, Title 1).
The VerticalLayout contains 2 GroupBoxes (Title 2 and Title 3).
I decided to go with a VerticalLayout in a HorizontalLayout and not a GridLayout since the Title 3 GroupBox can be disabled.
This makes it so the Title 2 GroupBox will fill in the space when the Title 3 GroupBox is hidden.As you can see in the image, the Title 2 GroupBox isn't aligned properly with the Title 1 GroupBox.
I would like them to be at the same height, so when the Title 3 GroupBox is hidden, the Title 2 and Title 1 GroupBoxes would be the same exact size.I tried setting the contentMargins to 0 on all the components mentioned.
HorizontalLayout.setContentsMargins(0, 0, 0, 0)
VerticalLayout.setContentsMargins(0, 0, 0, 0)
Title2GroupBox.setContentsMargins(0, 0, 0, 0)
Title3GroupBox.setContentsMargins(0, 0, 0, 0)Nothing seems to change at all. I also tried playing with the spacing but would like to keep the spacing between Title 2 GroupBox and Title 3 GroupBox as it is currently.
In my testing, the spacing didn't affect what I am trying to edit at all. Nothing seemed to affect the space I am trying to remove.Thanks for the help!
Here is a code snippet to help visualize what I did.
self.horizontalLayout = QHBoxLayout() self.horizontalLayout.setObjectName(u"horizontalLayout") self.horizontalLayout.setContentsMargins(0, 0, 0, 0) self.verticalLayout = QVBoxLayout() self.verticalLayout.setObjectName(u"verticalLayout") self.verticalLayout.setContentsMargins(0, 0, 0, 0) self.horizontalLayout.addLayout(self.verticalLayout) self.verticalLayout.addWidget(self.groupBox_Title2) self.verticalLayout.addWidget(self.groupBox_Title3) self.horizontalLayout.addWidget(self.groupBox_Title1)
-
Hi,
What about putting groupBox_Title1 in its own vertical layout ?
-
Hi SGaist,
Thanks for the suggestion, this seems to work just fine.