3

I created a toolbar in a plugin with:

toolbar = iface.addToolBar("new Toolbar")

I know I can toggle visibility of this toolbar with:

toolbar.setVisible(False)

and

toolbar.setVisible(True)

But for that I need direct access to the Variable toolbar

Now I want to write a new plugin which changes visibility of some toolbars.

From the new toolbar i cant access the variable toolbar.

So I tried to access the Toolbar with

iface.mainWindow().findChild(QToolBar, 'new Toolbar')

But it only returns None. I also don't have a Tooltip when I hover over the Toolbar. QGIS Standard Toolbars show a Tooltip on Hover...

It seems the Name of the toolbar isn't really saved.

How can I define a Name for a toolbar, so I can access it later from other scripts or is there a way to access it with 'new Toolbar'?

Also it would be nice to find identifiers for accessing toolbars of third party plugins I installed for QGIS.

1 Answer 1

5

You have to use the PyQt method :

toolbar.setObjectName("new Toolbar")

now, the following :

iface.mainWindow().findChild(QToolBar,'new Toolbar')

will return an object, your toolbar.

Or, if you want to search the toolbar by its window title, set at its instanciation, you can do :

searched_toolbars = [
    t for t in iface.mainWindow().findChildren(QToolBar)
    if t.windowTitle() == "new Toolbar"
]

if len(searched_toolbars) > 0:
    my_toolbar = searched_toolbars[0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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