Friday, 9 December 2016

pyqgis - Finding the children name of menubar in QGIS Python


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?



Answer



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














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


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...