I wrote a project macro which, when loaded, hides all toolbars except for one which contains a couple of my plugin buttons. I know it's possible to trigger core buttons like the following example:


But is it possible to do this for a custom-made plugin?

I noticed I could identify the objects of the toolbar using:

from PyQt4.QtGui import QToolBar
for x in iface.mainWindow().findChildren(QToolBar): 
    # x.objectName() returns the names of the toolbars
    if x.objectName() == 'Example':
        print x.children()

[<PyQt4.QtGui.QLayout object at 0x0000000023589840>, <PyQt4.QtGui.QToolButton object at 0x0000000024DCE488>, <PyQt4.QtGui.QAction object at 0x0000000024DCE510>, 
<PyQt4.QtGui.QToolButton object at 0x0000000024DCE598>, <PyQt4.QtGui.QToolButton object at 0x0000000024DCE620>]

But unsure how to trigger the buttons...


It seems that this is more a PyQt4 question.

I guess the best way of doing it is to give your actions an objectName, so that you can access them directly instead of looping through toolbars and their children.

For instance, I can give the custom name mActionCopyLayersToClipboard to one of my plugin buttons:

self.actionCopy = QAction( icon, u"Copy selected layers and groups to  clipboard (Ctrl+Ins)", self.iface.mainWindow() )

And then I can trigger it in this way:

from PyQt4.QtGui import QAction
iface.mainWindow().findChild( QAction, 'mActionCopyLayersToClipboard' ).trigger()
| improve this answer | |

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.