From 890816551f954bf78b2e73ae96b37e99c3c4f84c Mon Sep 17 00:00:00 2001 From: Talley Lambert Date: Fri, 20 Nov 2026 14:37:59 -0500 Subject: [PATCH] fix: remove dead _disconnect hooks on QModelMenu/QModelToolBar `destroyed.connect(self._disconnect)` never actually fires. It isn't needed: psygnal holds the slot weakly, so the connection goes away when the python wrapper is collected. Also suppress RuntimeError in QModelToolBar._on_registry_changed (as QModelMenu already does), for the case where the C++ object is deleted but the wrapper is still alive. closes #157 --- src/app_model/backends/qt/_qmenu.py | 13 ++++--------- tests/test_qt/test_qmenu.py | 28 ++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/app_model/backends/qt/_qmenu.py b/src/app_model/backends/qt/_qmenu.py index 3a929de2..34496261 100644 --- a/src/app_model/backends/qt/_qmenu.py +++ b/src/app_model/backends/qt/_qmenu.py @@ -48,7 +48,6 @@ def __init__( self.setObjectName(menu_id) self.rebuild() self._app.menus.menus_changed.connect(self._on_registry_changed) - self.destroyed.connect(self._disconnect) # ---------------------- if title is not None: @@ -98,9 +97,6 @@ def _on_about_to_show(self) -> None: if isinstance(action, QCommandRuleAction): action._refresh() - def _disconnect(self) -> None: - self._app.menus.menus_changed.disconnect(self._on_registry_changed) - def _on_registry_changed(self, changed_ids: set[str]) -> None: if self._menu_id in changed_ids: # if this (sub)menu has been removed from the registry, @@ -202,7 +198,6 @@ def __init__( self.setObjectName(menu_id) self.rebuild() self._app.menus.menus_changed.connect(self._on_registry_changed) - self.destroyed.connect(self._disconnect) # ---------------------- if title is not None: @@ -249,12 +244,12 @@ def rebuild( exclude=self._exclude if exclude is None else exclude, ) - def _disconnect(self) -> None: - self._app.menus.menus_changed.disconnect(self._on_registry_changed) - def _on_registry_changed(self, changed_ids: set[str]) -> None: if self._menu_id in changed_ids: - self.rebuild() + # the C++ object may already be deleted while the python wrapper + # (and therefore this weakly-connected slot) is still alive. + with contextlib.suppress(RuntimeError): + self.rebuild() class QModelMenuBar(QMenuBar): diff --git a/tests/test_qt/test_qmenu.py b/tests/test_qt/test_qmenu.py index 359a484c..ed1ff0a3 100644 --- a/tests/test_qt/test_qmenu.py +++ b/tests/test_qt/test_qmenu.py @@ -1,16 +1,18 @@ from __future__ import annotations +import gc import sys from typing import TYPE_CHECKING, cast import pytest -from qtpy.QtCore import Qt +from qtpy.QtCore import QEvent, Qt from qtpy.QtWidgets import QAction, QMainWindow from app_model.backends.qt import QModelMenu, QModelToolBar if TYPE_CHECKING: from pytestqt.plugin import QtBot + from qtpy.QtWidgets import QApplication from conftest import FullApp @@ -64,7 +66,29 @@ def test_menu( with pytest.raises(NameError, match="Names required to eval this expression"): menu.update_from_context({}) - menu._disconnect() + +@pytest.mark.parametrize("MenuCls", [QModelMenu, QModelToolBar]) +def test_menu_cpp_deleted( + MenuCls: type[QModelMenu] | type[QModelToolBar], + qapp: QApplication, + full_app: FullApp, +) -> None: + """Registry changes are safe after the C++ object is deleted (#157).""" + app = full_app + signal = app.menus.menus_changed + n_slots = len(signal) + menu = MenuCls(app.Menus.EDIT, app) + assert len(signal) == n_slots + 1 + + menu.deleteLater() + qapp.sendPostedEvents(None, QEvent.Type.DeferredDelete) + # python wrapper is still alive, so the slot is still connected + signal.emit({app.Menus.EDIT}) + + # once the wrapper is gone, the (weak) connection cleans itself up + del menu + gc.collect() + assert len(signal) == n_slots def test_submenu(qtbot: QtBot, full_app: FullApp) -> None: