corvus
corvus

Reputation: 595

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?

Upvotes: 1

Views: 1057

Answers (2)

couteau
couteau

Reputation: 319

If anyone is still looking for an answer to this question, and has a need to remove the toolbar without destroying it, you can remove the toolbar from the View/Toolbar menu as follows.

toolbarMenu = iface.mainWindow().findChild(QMenu, 'mToolbarMenu')
toolbarMenu.removeAction(toolbar.toggleViewAction())

To remove the toolbar from the toolbar area, I found that iface.mainWindow().removeToolBar(toolbar) did not work if the toolbar is not destroyed. I had to call toolbar.hide() and toolbar.setParent(None) to force the toolbar to actually disappear from the QGIS toolbar area.

Upvotes: 1

CodeBard
CodeBard

Reputation: 276

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()

Upvotes: 1

Related Questions