Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/app_model/backends/qt/_qaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ 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))
Expand Down
18 changes: 17 additions & 1 deletion tests/test_qt/test_qactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -16,7 +17,6 @@
)

if TYPE_CHECKING:
from app_model import Application
from conftest import FullApp


Expand Down Expand Up @@ -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
Loading