From a06f0600c1c9a80436f01533a82905a6f3633895 Mon Sep 17 00:00:00 2001 From: appel_c Date: Wed, 16 Apr 2025 16:24:11 +0200 Subject: [PATCH] fix(dark-mode-button): fix parent passed to QObjects in various classes --- bec_widgets/applications/launch_window.py | 2 +- bec_widgets/utils/expandable_frame.py | 2 +- bec_widgets/utils/toolbar.py | 20 ++++++++++--------- .../widgets/containers/dock/dock_area.py | 4 +++- .../dark_mode_button/dark_mode_button.py | 4 ++-- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/bec_widgets/applications/launch_window.py b/bec_widgets/applications/launch_window.py index 3f2d6c2b..311a3af4 100644 --- a/bec_widgets/applications/launch_window.py +++ b/bec_widgets/applications/launch_window.py @@ -146,7 +146,7 @@ class LaunchWindow(BECMainWindow): self.dark_mode_button = DarkModeButton(parent=self, toolbar=True) self.toolbar = ModularToolBar(parent=self) self.addToolBar(Qt.TopToolBarArea, self.toolbar) - self.spacer = QWidget() + self.spacer = QWidget(self) self.spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.toolbar.addWidget(self.spacer) self.toolbar.addWidget(self.dark_mode_button) diff --git a/bec_widgets/utils/expandable_frame.py b/bec_widgets/utils/expandable_frame.py index 6bd42eed..2f5f994a 100644 --- a/bec_widgets/utils/expandable_frame.py +++ b/bec_widgets/utils/expandable_frame.py @@ -37,7 +37,7 @@ class ExpandableGroupFrame(QFrame): self._title_layout.addWidget(self._expansion_button) self._title_layout.addWidget(self._title) - self._contents = QWidget() + self._contents = QWidget(self) self._layout.addWidget(self._contents) self._expansion_button.clicked.connect(self.switch_expanded_state) diff --git a/bec_widgets/utils/toolbar.py b/bec_widgets/utils/toolbar.py index 21a9ec1a..51f8f911 100644 --- a/bec_widgets/utils/toolbar.py +++ b/bec_widgets/utils/toolbar.py @@ -118,7 +118,7 @@ class IconAction(ToolBarAction): def add_to_toolbar(self, toolbar: QToolBar, target: QWidget): icon = QIcon() icon.addFile(self.icon_path, size=QSize(20, 20)) - self.action = QAction(icon, self.tooltip, target) + self.action = QAction(icon=icon, text=self.tooltip, parent=target) self.action.setCheckable(self.checkable) toolbar.addAction(self.action) @@ -128,7 +128,7 @@ class QtIconAction(ToolBarAction): super().__init__(icon_path=None, tooltip=tooltip, checkable=checkable) self.standard_icon = standard_icon self.icon = QApplication.style().standardIcon(standard_icon) - self.action = QAction(self.icon, self.tooltip, parent) + self.action = QAction(icon=self.icon, text=self.tooltip, parent=parent) self.action.setCheckable(self.checkable) def add_to_toolbar(self, toolbar, target): @@ -173,7 +173,7 @@ class MaterialIconAction(ToolBarAction): filled=self.filled, color=self.color, ) - self.action = QAction(self.icon, self.tooltip, parent=parent) + self.action = QAction(icon=self.icon, text=self.tooltip, parent=parent) self.action.setCheckable(self.checkable) def add_to_toolbar(self, toolbar: QToolBar, target: QWidget): @@ -212,12 +212,12 @@ class DeviceSelectionAction(ToolBarAction): self.device_combobox.currentIndexChanged.connect(lambda: self.set_combobox_style("#ffa700")) def add_to_toolbar(self, toolbar, target): - widget = QWidget() + widget = QWidget(parent=target) layout = QHBoxLayout(widget) layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(0) if self.label is not None: - label = QLabel(f"{self.label}") + label = QLabel(text=f"{self.label}", parent=target) layout.addWidget(label) if self.device_combobox is not None: layout.addWidget(self.device_combobox) @@ -280,7 +280,9 @@ class SwitchableToolBarAction(ToolBarAction): self.main_button.clicked.connect(self._trigger_current_action) menu = QMenu(self.main_button) for key, action_obj in self.actions.items(): - menu_action = QAction(action_obj.get_icon(), action_obj.tooltip, self.main_button) + menu_action = QAction( + icon=action_obj.get_icon(), text=action_obj.tooltip, parent=self.main_button + ) menu_action.setIconVisibleInMenu(True) menu_action.setCheckable(self.checkable) menu_action.setChecked(key == self.current_key) @@ -369,13 +371,13 @@ class WidgetAction(ToolBarAction): toolbar (QToolBar): The toolbar to add the widget to. target (QWidget): The target widget for the action. """ - self.container = QWidget() + self.container = QWidget(parent=target) layout = QHBoxLayout(self.container) layout.setContentsMargins(0, 0, 0, 0) layout.setSpacing(0) if self.label is not None: - label_widget = QLabel(f"{self.label}") + label_widget = QLabel(text=f"{self.label}", parent=target) label_widget.setSizePolicy(QSizePolicy.Minimum, QSizePolicy.Fixed) label_widget.setAlignment(Qt.AlignVCenter | Qt.AlignRight) layout.addWidget(label_widget) @@ -437,7 +439,7 @@ class ExpandableMenuAction(ToolBarAction): ) menu = QMenu(button) for action_id, action in self.actions.items(): - sub_action = QAction(action.tooltip, target) + sub_action = QAction(text=action.tooltip, parent=target) sub_action.setIconVisibleInMenu(True) if action.icon_path: icon = QIcon() diff --git a/bec_widgets/widgets/containers/dock/dock_area.py b/bec_widgets/widgets/containers/dock/dock_area.py index f39aa8b3..800023bf 100644 --- a/bec_widgets/widgets/containers/dock/dock_area.py +++ b/bec_widgets/widgets/containers/dock/dock_area.py @@ -181,7 +181,7 @@ class BECDockArea(BECWidget, QWidget): self.layout.addWidget(self.toolbar) self.layout.addWidget(self.dock_area) - self.spacer = QWidget() + self.spacer = QWidget(parent=self) self.spacer.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding) self.toolbar.addWidget(self.spacer) self.toolbar.addWidget(self.dark_mode_button) @@ -440,6 +440,8 @@ class BECDockArea(BECWidget, QWidget): Cleanup the dock area. """ self.delete_all() + self.dark_mode_button.close() + self.dark_mode_button.deleteLater() super().cleanup() def show(self): diff --git a/bec_widgets/widgets/utility/visual/dark_mode_button/dark_mode_button.py b/bec_widgets/widgets/utility/visual/dark_mode_button/dark_mode_button.py index 72a9e9bd..e8f352e8 100644 --- a/bec_widgets/widgets/utility/visual/dark_mode_button/dark_mode_button.py +++ b/bec_widgets/widgets/utility/visual/dark_mode_button/dark_mode_button.py @@ -31,9 +31,9 @@ class DarkModeButton(BECWidget, QWidget): self.layout.setAlignment(Qt.AlignmentFlag.AlignVCenter) if toolbar: - self.mode_button = QToolButton() + self.mode_button = QToolButton(parent=parent) else: - self.mode_button = QPushButton() + self.mode_button = QPushButton(parent=parent) self.dark_mode_enabled = self._get_qapp_dark_mode_state() self.update_mode_button()