Search code examples
pythonqtpyqtqgispyqgis

Remove Toolbar from QGIS Toolbars Menu


I'd like to know how to fully remove a toolbar in PyQGIS, so that the toolbar is not only no longer visible in the toolbar area, but also no longer listed under the View menu (View > Toolbars) or when right-clicking on the toolbar area.

It is easy enough to remove a toolbar from the main window toolbar area using iface.mainWindow().removeToolBar(toolbar). This will also remove it from the listing that is shown when right-clicking on the toolbar area. However, it will not remove the toolbar from the View/Toolbar menu.

toolbar = QToolBar('Test Toolbar')
# Add to main window & to 'View' menu
iface.addToolBar(toolbar)
# Remove from main window
iface.mainWindow().removeToolBar(toolbar)
# 'Test toolbar' is still visible in 'View' menu

How can I make it so that the toolbar is no longer accessible from the UI?


Solution

  • calling deleteLater() on the toolbar object schedules it for deletion and completely removes it also from the view -> toolbars menu. Note that you won't be able to further use the toolbar after that, for example re-adding it with iface.addToolBar(toolbar) will not work.

    toolbar = QToolBar('Test Toolbar')
    # Add to main window & to 'View' menu
    iface.addToolBar(toolbar)
    # Remove from main window & 'View' menu
    toolbar.deleteLater()