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.

Taras
35.8k5 gold badges77 silver badges152 bronze badges
asked Aug 30, 2022 at 9:10

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]
Taras
35.8k5 gold badges77 silver badges152 bronze badges
answered Aug 30, 2022 at 9:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.