J_yang
J_yang

Reputation: 2832

PyQt, How to change BoxLayout's weight (size)

I have a Gui layout as in the picture. enter image description here

So currently Sublayout1 and Sublayout2 are equal in size. However I want to make Sublayout1's width smaller and stretch Sublayout2's width bigger. Is there a way to do it dynamically instead of putting a fixed size. Like on Android Studio where you can set the weight of the elements within the layout.

Also affecting the size shouldn't affect the Bottomlayout either. Many thanks for your help. A snippet of the code is:

sublayout1 = QtGui.QVBoxLayout()
sublayout2 = QtGui.QVBoxLayout()
plotBox = QtGui.QHBoxLayout()
plotBox.addLayout(sublayout1)
plotBox.addLayout(sublayout2) 
bottomlayout = QtGui.QHBoxLayout()
mainlayout.addLayout(plotBox)
mainlayout.addLayout(bottomlayout)

Upvotes: 5

Views: 12611

Answers (1)

G.M.
G.M.

Reputation: 12909

Use the stretch parameter passed to QBoxLayout::addLayout

sublayout1 = QtGui.QVBoxLayout()
sublayout2 = QtGui.QVBoxLayout()
plotBox = QtGui.QHBoxLayout()
plotBox.addLayout(sublayout1, 1)
plotBox.addLayout(sublayout2, 2)
bottomlayout = QtGui.QHBoxLayout()
mainlayout.addLayout(plotBox)
mainlayout.addLayout(bottomlayout)

In the above sublayout2 has a stretch twice that of sublayout1 and so should be allocated more space.

Upvotes: 13

Related Questions