panofish
panofish

Reputation: 7889

Iterate QAction items on a given QToolBar object in PyQt?

Using Python and PyQt4, given a GUI with any number of QToolBar objects and each toolbar contains any number of QAction objects.

I can iterate the ui with the following code and find the toolbars:

for name, obj in inspect.getmembers(ui):
    if isinstance(obj, QToolBar):
        print "toolbar =",name

How can I iterate each toolbar object and find the QAction objects. I will then read the QActions to determine which are "checked". I am not using QActionGroups.

Upvotes: 1

Views: 2803

Answers (2)

ekhumoro
ekhumoro

Reputation: 120608

If the toolbars were created in Qt Designer, they will become children of the main window (or whatever the top-level widget is).

So you can simply do:

for toolbar in mainwindow.findChildren(QToolBar):
    print('toolbar: %s' % toolbar.objectName())
    for action in toolbar.actions():
        if not action.isSeparator():
            print('  action: %s (%s)' % (action.text(), action.isChecked()))

Upvotes: 5

101
101

Reputation: 8999

Given a QToolBar you can find all of its QActions by calling its actions method:

Returns the (possibly empty) list of this widget's actions.

For example:

if isinstance(obj, QToolBar):
    print obj.actions()

Upvotes: 2

Related Questions