3

I have try the code in Finding name of QGIS toolbar in Python? and Name of children of toolbar in QGIS python and how to disable? to show the children name of menuBar. But, get error. The children name of menuBar are : Project, Edit, View, Layer, Settings, Plugins, Vector, Raster, Database, Web, Processing, and Help.

I have input the code :

from PyQt4.QtGui import QToolBar, QDockWidget, QMenuBar
for x in iface.mainWindow().findChildren(QMenuBar):
    print x.objectName()

until here, it is work. The output is menubar. When I type the code :

for icon in iface.menubar().actions(): 
    print icon.objectName()

It gives error : AttributeError: 'QgisInterface' object has no attribute 'menubar'.

To test the name is true, I want to remove many of children name. For example, I want to remove Edit, View, Layer, and Settings. So, how the code in python?

  • 1
    Please edit your question to include a snippet of the code you've tried, a description of what actually happens when you try it, and the full error message as text. – Midavalo Feb 24 '17 at 1:28
1

It's correct because u'menubar' is only a name; not an object. Your object is x (specifically a QMenuBar object) and it has effectively an 'actions' method. So, you can do:

from PyQt4.QtGui import QToolBar, QDockWidget, QMenuBar

for x in iface.mainWindow().findChildren(QMenuBar):
    print x.objectName() #redundant because there is only one menubar

for item in x.actions():
    print item

and it's printed:

menubar
<PyQt4.QtGui.QAction object at 0x90baa1dc>
<PyQt4.QtGui.QAction object at 0x90baa53c>
<PyQt4.QtGui.QAction object at 0x90baa584>
<PyQt4.QtGui.QAction object at 0x90baa5cc>
<PyQt4.QtGui.QAction object at 0x90baa614>
<PyQt4.QtGui.QAction object at 0x90baa65c>
<PyQt4.QtGui.QAction object at 0x90baa6a4>
<PyQt4.QtGui.QAction object at 0x90baa6ec>
<PyQt4.QtGui.QAction object at 0x90baa734>
<PyQt4.QtGui.QAction object at 0x90baa77c>
<PyQt4.QtGui.QAction object at 0x90baa7c4>
<PyQt4.QtGui.QAction object at 0x90fdc6a4>

Each one of 12 objects is a QAction object; not a QIcon object. If you want icon name's then:

for i, item in enumerate(x.actions()):
    print i+1, 'name: ', item.icon().name()

but nothing it's printed as 'name' because there is not any icon associated to twelve Menu Bar items.

enter image description here

  • 1
    You're right in that you cannot print the name but you can print the text by using print item.text(). This will give the text of each QAction but you should notice that they will also contain the mnemonic ampersand (&) which is used for shortcuts. So if the OP wants to hide the Edit option, they could use: for item in x.actions(): if item.text().replace('&','') == 'Edit': item.setVisible(False). – Joseph Feb 24 '17 at 11:18
  • 1
    @Joseph Thanks a lot for your advice. The item.icon() is a QIcon object and it have 'name' method but, nothing it's printed as 'name' because there is not any icon associated to twelve Menu Bar items. You're completely right in that you can print the text of each QAction by using print item.text() but, based at his second loop, I interpreted that its main objective was icon name. – xunilk Feb 24 '17 at 11:48
  • The code is work when I type in Python Console. QGIS must load first. Then I open Python Console. But, when I put the code in startup.py, I don't know why 'Database', 'Web', and 'Processing' are still there. – mega Feb 26 '17 at 23:59
  • Ok. It is work. It is based on gis.stackexchange.com/questions/230320/… – mega Mar 1 '17 at 23:52

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.