Re-writing the CollapsibleBox box to use the visibility flag should reduce the amount of lines of code and remove any issues set by trying to juggle the collapsed maximum height etc.
You can also use resizeEvent to get the height of the flow layout and set that to be the max height to make sure the box is using as little space as needed.
def __init__(self, title="", parent=None):
super(CollapsibleBox, self).__init__(parent)
self.toggle_button = QtWidgets.QToolButton(text=title, checkable=True, checked=False)
self.toggle_button.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon)
self.toggle_button.setArrowType(QtCore.Qt.RightArrow)
self.toggle_button.pressed.connect(self.on_pressed)
self.toggle_animation = QtCore.QParallelAnimationGroup(self)
self.content_area = QtWidgets.QScrollArea()
lay = QtWidgets.QVBoxLayout(self)
lay.setSpacing(0)
lay.setContentsMargins(0, 0, 0, 0)
lay.addWidget(self.toggle_button)
lay.addWidget(self.content_area)
self.content_area.setVisible(False)
self.setSizePolicy(QtWidgets.QSizePolicy.MinimumExpanding, QtWidgets.QSizePolicy.MinimumExpanding)
def on_pressed(self):
checked = self.toggle_button.isChecked()
print(checked)
self.toggle_button.setArrowType(
QtCore.Qt.DownArrow if not checked else QtCore.Qt.RightArrow
)
self.content_area.setVisible(not checked)
def setContentLayout(self, layout):
lay = self.content_area.layout()
del lay
self.content_area.setLayout(layout)
def resizeEvent(self, event):
self.content_area.setMaximumHeight((self.content_area.layout().heightForWidth(self.width())))
return super(CollapsibleBox, self).resizeEvent(event)
It is a little confusing that you use a flowlayout (that will expand and contract the widgets to fit the given space) inside of a QScrollArea (which will give you scrollbars to see all the widgets as it expects them to be outside of the given space) as only one would come into play by default.
Depending on the effect your after, you might want to either swap out the QScrollArea for a QWidget if you don’t need to scroll, or add a hard limit on X/Y where the scroll bars come into play.