1

For a Python plugin I need to remove/disable some UI elements so that they can't be accessed by the user. Thus, I removed the respective actions, toolbars and menus from the interface.

However, in order to prevent the user from turning back on the disabled elements, their corresponding menu entries also have to be removed. Unfortunately, I didn't find a way to access the viewmenu's entries "Toolbars" and "Panels" (which contain the desired actions). They are not returned when calling the viewmenu's children() method. Likewise, the findChild()/findChildren() methods do not return anything helpful either.

I already had a look on the QGIS git repository and it seems that the desired menus and actions are not added to the viewmenu as its child elements. They seem to do not have any parent element at all..

So does anybody know how I can access the "Panels" and "Toolbars" menus from out my plugin code?

2
  • Perhaps this post might help: How to customize the QGIS GUI using Python?
    – Joseph
    Jun 21, 2016 at 10:30
  • 1
    Thanks for your comment Joseph, but I think the desired menus/actions are not accessible through the "standard" way. I already tried to access the menus through the iface, mainWindow and menuBar classes.
    – TobsenB
    Jun 21, 2016 at 10:35

2 Answers 2

3

Try using the following:

# statement for PyQt4
from PyQt4.QtGui import QDockWidget, QMenu, QToolBar
# Replace with the following line if QGIS 3
# from qgis.PyQt.QtWidgets import QDockWidget, QMenu, QToolBar

main_window = iface.mainWindow()

# To get "Panels" (in fact, we echoed their title)
print([i.windowTitle() for i in main_window.findChildren(QDockWidget)])
# To get "Toolbars" (in fact, we echoed their title)
print([i.windowTitle() for i in main_window.findChildren(QToolBar)])
# If quickosm plugin installed, remove it from the toolbar with
if len([i for i in main_window.findChildren(QToolBar) if i.windowTitle() == u'QuickOSM']) > 0:
    main_window.removeToolBar([i for i in main_window.findChildren(QToolBar) if i.windowTitle() == u'QuickOSM'][0])

# To get menus named "Panels" and "Toolbars"
print([i.title() for i in main_window.findChildren(QMenu) if i.title() in ['Panels', 'Toolbars']])
4
  • Thanks for your help, but the menus seem to be not child elements of the mainWindow class.
    – TobsenB
    Jun 27, 2016 at 9:45
  • Yes but you can find them with print([i.parent() for i in main_window.findChildren(QMenu) if i.title() in ['Panels', 'Toolbars']]). findChildren doesn't look only at direct childs.
    – ThomasG77
    Jun 27, 2016 at 10:18
  • This code gives an error in QGIS 3.10 , as it can't import QDockWidget, QMenu, QToolBar, and also shows an error of IndexError: list index out of range. How to solve this is QGIS 3.10? Sep 4, 2020 at 9:28
  • See my edit where I mention about changing import for QGIS 3. I've also put an if for QuickOSM as you run the code and you don't have QuickOSM (it's responsible of the error IndexError: list index out of range you mentioned)
    – ThomasG77
    Sep 4, 2020 at 12:12
2

I now found a solution to my problem, although it's not the most elegant one. I got access to the elements by calling the actions() method of the view menu:

vm = iface.viewMenu()
panels = [i for i in vm.actions() if i.title() == 'Panels'][0]
toolbars = [i for i in vm.actions() if i.title() == 'Toolbars'][0]

Unfortunately the plugin will be used in multiple languages, so that the solution is not as generic as it should be. However, this should do it for the moment. Thanks for your answers!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.