Skip to content

removePluginMenu leaves a hidden empty menu #66186

Description

@eithanwes

What is the bug or the crash?

when a plugin menu is empty after using iface.removePluginMenu the menu is removed from the plugin menu but it is not deleted and stays hidden and alive until the program is closed. meaning that if a plugin is unloaded and reloaded multiple time every time accumulates a new orphan menu.

same goes for removePluginDatabaseMenu(), removePluginMenu(), removePluginMeshMenu(), removePluginRasterMenu(), removePluginVectorMenu(), removePluginWebMenu()

Steps to reproduce the issue

I ran this code in the console:


from qgis.PyQt.QtWidgets import QAction, QMenu
from qgis.PyQt.QtCore import QCoreApplication, QEvent

menu_name = "&Repro Menu"

def dump():
    menus = [
        menu for menu in iface.mainWindow().findChildren(QMenu)
        if menu.title() == menu_name
    ]
    print(f"found {len(menus)} menu(s)")
    for index, menu in enumerate(menus, 1):
        print(index, menu, "actions =", [action.text() for action in menu.actions()])

print("before:")
dump()

action = QAction("Temporary Action", iface.mainWindow())
action2 = QAction("Temporary Action2", iface.mainWindow())
iface.addPluginToMenu(menu_name, action)
iface.addPluginToMenu(menu_name, action2)

print("\nafter add:")
dump()

iface.removePluginMenu(menu_name, action)
iface.removePluginMenu(menu_name, action2)
action.deleteLater()
QCoreApplication.sendPostedEvents(None, QEvent.Type.DeferredDelete)
QCoreApplication.processEvents()

print("\nafter remove:")
dump()

every time this is run there is another lonely QMenu with the same name.
output after running a few times:

before:
found 3 menu(s)
1 <PyQt5.QtWidgets.QMenu object at 0x000002A106B62F00> actions = []
2 <PyQt5.QtWidgets.QMenu object at 0x000002A106B62F90> actions = []
3 <PyQt5.QtWidgets.QMenu object at 0x000002A106B63020> actions = []

after add:
found 4 menu(s)
1 <PyQt5.QtWidgets.QMenu object at 0x000002A106B62D50> actions = []
2 <PyQt5.QtWidgets.QMenu object at 0x000002A106B62DE0> actions = []
3 <PyQt5.QtWidgets.QMenu object at 0x000002A106B62E70> actions = []
4 <PyQt5.QtWidgets.QMenu object at 0x000002A106B639B0> actions = ['Temporary Action', 'Temporary Action2']

after remove:
found 4 menu(s)
1 <PyQt5.QtWidgets.QMenu object at 0x000002A106B62B10> actions = []
2 <PyQt5.QtWidgets.QMenu object at 0x000002A106B62BA0> actions = []
3 <PyQt5.QtWidgets.QMenu object at 0x000002A106B62C30> actions = []
4 <PyQt5.QtWidgets.QMenu object at 0x000002A106B62CC0> actions = []

Versions

QGIS version3.44.10-Solothurn
QGIS code revision565fe70b
 
Libraries
Qt version5.15.13
Python version3.12.13
GDAL version3.12.4 — Chicoutimi
PROJ version9.8.1
EPSG Registry database versionv12.029 (2025-10-02)
GEOS version3.14.1-CAPI-1.20.5
SQLite version3.53.0
PDAL version2.10.0
PostgreSQL client version17.3
SpatiaLite version5.1.0
QWT version6.3.0
QScintilla2 version2.14.1
OS versionWindows 11 Version 2009
 
Active Python plugins
attributeshortcut1.1.1
autoSaver2.9
plugin_reloader0.22
qgis_mcp_plugin1.0
SelectionFilter2.0
processing2.12.99
QGIS version4.0.2-Norrköping
QGIS code revisionbb929340
 
Libraries
Qt version6.11.0
Python version3.12.13
GDAL version3.12.4 — Chicoutimi
PROJ version9.8.1
EPSG Registry database versionv12.029 (2025-10-02)
GEOS version3.14.1-CAPI-1.20.5
SFCGAL version2.2.0
GeographicLib version2.7.0
SQLite version3.53.0
PDAL version2.10.0
PostgreSQL client version17.3
SpatiaLite version5.1.0
QWT version6.3.0
QScintilla2 version2.14.1
OS versionWindows 11 Version 25H2
 
Active Python plugins
MetaSearch0.3.6
db_manager0.1.20
grassprovider2.12.99
plugin_reloader0.22
processing2.12.99

Supported QGIS version

  • I'm running a supported QGIS version according to the roadmap.

New profile

No Duplicate of the Issue

  • I have verified that there are no existing issues raised related to my problem.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugEither a bug report, or a bug fix. Let's hope for the latter!PluginsPyQGISRelated to the PyQGIS API

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions