diff --git a/magicgui/backends/_qtpy/widgets.py b/magicgui/backends/_qtpy/widgets.py index fcf038070..72b9269b4 100644 --- a/magicgui/backends/_qtpy/widgets.py +++ b/magicgui/backends/_qtpy/widgets.py @@ -386,6 +386,22 @@ def __init__(self, layout="vertical"): self._layout = QtW.QVBoxLayout() self._qwidget.setLayout(self._layout) + def _mgui_set_scrollable(self, scrollable: bool): + if scrollable == self._mgui_get_scrollable(): + return + + if scrollable: + self._scroll = QtW.QScrollArea() + self._scroll.setWidget(self._qwidget) + self._scroll.setWidgetResizable(False) + self._qwidget = self._scroll + if not scrollable: + self._qwidget = self._scroll.widget() + del self._scroll + + def _mgui_get_scrollable(self): + return hasattr(self, "_scroll") + def _mgui_insert_widget(self, position: int, widget: Widget): self._layout.insertWidget(position, widget.native) diff --git a/magicgui/widgets/_bases/container_widget.py b/magicgui/widgets/_bases/container_widget.py index c554f02b9..9ef98828c 100644 --- a/magicgui/widgets/_bases/container_widget.py +++ b/magicgui/widgets/_bases/container_widget.py @@ -216,6 +216,15 @@ def layout(self, value): "It is not yet possible to change layout after instantiation" ) + @property + def scrollable(self) -> bool: + """Return if the widget can have scroll bars.""" + return self._widget._mgui_get_scrollable() + + @scrollable.setter + def scrollable(self, value: bool): + self._widget._mgui_set_scrollable(value) + def reset_choices(self, *_: Any): """Reset choices for all Categorical subWidgets to the default state. diff --git a/magicgui/widgets/_protocols.py b/magicgui/widgets/_protocols.py index 043994e3f..1d8c5ea09 100644 --- a/magicgui/widgets/_protocols.py +++ b/magicgui/widgets/_protocols.py @@ -450,6 +450,14 @@ def _mgui_get_margins(self) -> tuple[int, int, int, int]: def _mgui_set_margins(self, margins: tuple[int, int, int, int]) -> None: raise NotImplementedError() + @abstractmethod + def _mgui_get_scrollable(self) -> bool: + raise NotImplementedError() + + @abstractmethod + def _mgui_set_scrollable(self, scrollable: bool) -> None: + raise NotImplementedError() + class MainWindowProtocol(ContainerProtocol, Protocol): """Application main widget."""