From 075e0b080f0ef8441d14d981e3a6cc69d8211439 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Weber?= Date: Tue, 28 Oct 2025 21:31:08 +0100 Subject: [PATCH] apply add_widget also to toolbar specified using str (missing in #117) --- src/pymodaq_gui/managers/action_manager.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/pymodaq_gui/managers/action_manager.py b/src/pymodaq_gui/managers/action_manager.py index af8ba033..207a35ea 100644 --- a/src/pymodaq_gui/managers/action_manager.py +++ b/src/pymodaq_gui/managers/action_manager.py @@ -287,7 +287,8 @@ def add_action(self, short_name: str = '', name: str = '', icon_name: Union[str, elif isinstance(toolbar, str): toolbar = self.get_toolbar(toolbar) elif not isinstance(toolbar, QtWidgets.QToolBar): - raise TypeError(f'toolbar must be either None, a string, or QToolBar, got {type(menu)}') + raise TypeError(f'toolbar must be either None, a string, or QToolBar, got {type(toolbar)}') + if auto_menu: if menu is None: menu = self._menu @@ -301,8 +302,8 @@ def add_action(self, short_name: str = '', name: str = '', icon_name: Union[str, return self._actions[short_name] def add_widget(self, short_name, klass: Union[str, QtWidgets.QWidget, object], *args, tip='', - toolbar: QtWidgets.QToolBar = None, visible=True, signal_str=None, - slot: Callable=None, enabled=True, **kwargs): + toolbar: Union[str, QtWidgets.QToolBar] = None, visible=True, signal_str=None, + slot: Callable=None, enabled=True, auto_toolbar=True, **kwargs): """Create and add a widget to a toolbar Parameters @@ -325,16 +326,25 @@ def add_widget(self, short_name, klass: Union[str, QtWidgets.QWidget, object], * a callable connected to the signal enabled: bool enable state of the widget + auto_toolbar: bool + if True add this action to the defined toolbar kwargs: dict variable named arguments passed as is to the widget constructor Returns ------- QtWidgets.QWidget """ - if toolbar is None: - toolbar = self._toolbar + if auto_toolbar: + if toolbar is None: + toolbar = self._toolbar + elif isinstance(toolbar, str): + toolbar = self.get_toolbar(toolbar) + elif not isinstance(toolbar, QtWidgets.QToolBar): + raise TypeError(f'toolbar must be either None, a string, or QToolBar, got {type(toolbar)}') + widget = addwidget(klass, *args, tip=tip, toolbar=toolbar, visible=visible, signal_str=signal_str, slot=slot, enabled=enabled, **kwargs) + if widget is not None: self._actions[short_name] = widget else: @@ -456,7 +466,7 @@ def actions(self) -> list[QAction]: def actions_names(self) -> list[str]: return list(self._actions.keys()) - def get_action(self, name) -> QAction: + def get_action(self, name) -> Union[QAction, QtWidgets.QWidget]: """Getter of a given action Parameters