From 90237fca00e8ff5e0f14ec83eb4019e27cb7465f Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Thu, 19 Nov 2026 22:11:35 +0100 Subject: [PATCH 1/2] fix: Clean QMenuItemAction parent on Application destroy --- src/app_model/backends/qt/_qaction.py | 9 +++++++++ tests/test_qt/test_qactions.py | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/app_model/backends/qt/_qaction.py b/src/app_model/backends/qt/_qaction.py index 6e96c07..98678d7 100644 --- a/src/app_model/backends/qt/_qaction.py +++ b/src/app_model/backends/qt/_qaction.py @@ -188,6 +188,15 @@ def __init__( with contextlib.suppress(NameError): self.update_from_context(self._app.context) + self._app.destroyed.connect(self._on_app_destroyed) + + def _on_app_destroyed(self) -> None: + # To not keep object alive after the app is destroyed, we remove the parent, which will + # trigger the deletion of this object. We ignore RuntimeError, which is raised if + # the Qt object has already been deleted. + with contextlib.suppress(RuntimeError): # Qt object already deleted + self.setParent(None) + @staticmethod def _cache_key(app: Application, menu_item: MenuItem) -> tuple[int, int]: return (id(app), hash(menu_item)) diff --git a/tests/test_qt/test_qactions.py b/tests/test_qt/test_qactions.py index 6d3cb2e..e1b19e3 100644 --- a/tests/test_qt/test_qactions.py +++ b/tests/test_qt/test_qactions.py @@ -4,6 +4,7 @@ import pytest from qtpy.QtGui import QColor +from app_model import Application from app_model.backends.qt import QCommandRuleAction, QMenuItemAction from app_model.types import ( Action, @@ -16,7 +17,6 @@ ) if TYPE_CHECKING: - from app_model import Application from conftest import FullApp @@ -202,3 +202,19 @@ def test_update_keybinding_in_tooltip( dispose1() q_action._update_keybinding() assert q_action.toolTip() == "Initial tooltip" + + +def test_clean_parent_on_app_destroyed(qapp) -> None: + app = Application("test_clean_parent") + action = Action( + id="test.clean.parent", + title="Test clean parent", + callback=lambda: None, + ) + app.register_action(action) + + q_action = QMenuItemAction.create(MenuItem(command=action), app, parent=qapp) + assert q_action.parent() is qapp + + Application.destroy(app.name) + assert q_action.parent() is None From 8a1e7171caad5acca09cd4c0b2c50f0d0ff71252 Mon Sep 17 00:00:00 2001 From: Grzegorz Bokota Date: Thu, 19 Nov 2026 22:31:40 +0100 Subject: [PATCH 2/2] shorter lines --- src/app_model/backends/qt/_qaction.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/app_model/backends/qt/_qaction.py b/src/app_model/backends/qt/_qaction.py index 98678d7..d83f6ce 100644 --- a/src/app_model/backends/qt/_qaction.py +++ b/src/app_model/backends/qt/_qaction.py @@ -191,8 +191,10 @@ def __init__( self._app.destroyed.connect(self._on_app_destroyed) def _on_app_destroyed(self) -> None: - # To not keep object alive after the app is destroyed, we remove the parent, which will - # trigger the deletion of this object. We ignore RuntimeError, which is raised if + # To not keep object alive after the app is destroyed, + # we remove the parent, which will + # trigger the deletion of this object. + # We ignore RuntimeError, which is raised if # the Qt object has already been deleted. with contextlib.suppress(RuntimeError): # Qt object already deleted self.setParent(None)