Reputation: 71
My app window has two parts in it. The top portion consists of several widgets contained inside a QVerticalLayout, which in turn contained in a QVerticalSpacer. This portion is always visible.
The bottom pane is contained in a GroupBox and may become invisible when user asks. Added a check box at the bottom of the screen, blow the group box. Clicking it toggle the visibility of the group box by calling it's hide() and show() methods.
Now, when the bottom pane is hidden, what can I do in order to shrink it's height, close as possible to zero? Or the dimensions of the tab that contains those two portions? or the entire QMainWindow?
Tried to toy with the SizePolicy of my groupbox. VerticalPolicy is the interesting property here. Tried to set it to Fixed and then to call groupbox.setGeometry() after calling groupbox.hide(). Seems that in that way I can't shrink the groupbox to 10 pixels or so. Can't make it smaller then the height of it's content.
Should I deplete the group box from it's content when hiding it and repopulate it in response to show? I hope there is a better way.
Upvotes: 0
Views: 836
Reputation: 10864
Either use a non-default size constraint on the layout:
layout.setSizeConstraint(QtGui.QLayout.SetFixedSize)
or call adjustSize after the hide:
widget.adjustSize()
A small example for PySide:
from PySide import QtGui
app = QtGui.QApplication([])
window = QtGui.QWidget()
window_layout = QtGui.QVBoxLayout(window)
window_layout.setSizeConstraint(QtGui.QLayout.SetFixedSize) # either this line
button = QtGui.QPushButton('Hide the box', window)
window_layout.addWidget(button)
box = QtGui.QGroupBox('A group')
box_layout = QtGui.QVBoxLayout(box)
box_layout.addWidget(QtGui.QLabel('Some text', box))
window_layout.addWidget(box)
def adjust():
box.hide()
window.adjustSize() # or that line do the trick
button.clicked.connect(adjust)
window.show()
app.exec_()
Upvotes: 1