From f4d9881b0c64c0419fa2da182a1c403a01bd084f Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Fri, 11 Mar 2022 14:16:03 -0500 Subject: [PATCH] Fix height of expanded QCollapsible when child changes size (#72) * update height when child changes * return false --- src/superqt/collapsible/_collapsible.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/superqt/collapsible/_collapsible.py b/src/superqt/collapsible/_collapsible.py index de4000f..d3b39d5 100644 --- a/src/superqt/collapsible/_collapsible.py +++ b/src/superqt/collapsible/_collapsible.py @@ -4,7 +4,9 @@ from typing import Optional from qtpy.QtCore import ( QAbstractAnimation, QEasingCurve, + QEvent, QMargins, + QObject, QPropertyAnimation, Qt, ) @@ -77,11 +79,13 @@ class QCollapsible(QFrame): def addWidget(self, widget: QWidget): """Add a widget to the central content widget's layout.""" + widget.installEventFilter(self) self._content.layout().addWidget(widget) def removeWidget(self, widget: QWidget): """Remove widget from the central content widget's layout.""" self._content.layout().removeWidget(widget) + widget.removeEventFilter(self) def expand(self, animate: bool = True): """Expand (show) the collapsible section""" @@ -126,3 +130,10 @@ class QCollapsible(QFrame): def _toggle(self): self.expand() if self.isExpanded() else self.collapse() + + def eventFilter(self, a0: QObject, a1: QEvent) -> bool: + """If a child widget resizes, we need to update our expanded height.""" + if a1.type() == QEvent.Type.Resize: + if self.isExpanded(): + self._expand_collapse(QAbstractAnimation.Direction.Forward) + return False