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?

  • Perhaps this post might help: How to customize the QGIS GUI using Python? – Joseph Jun 21 '16 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 '16 at 10:35
3

Try using the following:

from PyQt4.QtGui 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
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']])
  • Thanks for your help, but the menus seem to be not child elements of the mainWindow class. – TobsenB Jun 27 '16 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 '16 at 10:18
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, privacy policy and cookie policy

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