Questions about QToolbar/QToolButton
-
Hi,
I am experimenting a bit with QToolbar and QToolButtons.
Accidentally I noticed that the toolbar has a context menu that can be used to close the toolbar.- How can I avoid that the toolbar can be closed?
It should be floatable and moveable, but not closeable.
Another issue I discovered with the QToolButtons. When I apply setDefaultAction() to existing toolbutton, a down arrow appears, which means that the defaultAction has not been replaced but added.
- How can I replace the action of a QToolButton?
-
About the first question, according to the doc of QMainWindow:
The createPopupMenu() function creates popup menus when the main window receives context menu events. The default implementation generates a menu with the checkable actions from the dock widgets and toolbars. You can reimplement createPopupMenu() for a custom menu.
That's the menu you see, not from QToolbar, but from QMainWindow.
If you didn't need any context menu for the main window, you can simply callsetContextMenuPolicy(Qt::NoContextMenu)
to disable it (this will not affect the context menu of its child widgets). -
Hi,
@Bonnie said in Questions about QToolbar/QToolButton:
That's the menu you see, not from QToolbar, but from QMainWindow.
Ok, as it appears on floating toolbars too, I thought it was the menue of QToolbar.
@Bonnie said in Questions about QToolbar/QToolButton:
you can simply call setContextMenuPolicy(Qt::NoContextMenu)
Thank you very much!!!
That did the trick :)Regarding the second question - I tried lot of different ways - the only way to get desired visible behaviour is toolbar.clear() followed by recreating the entire toolbar from scratch.
I consider that way as absolutely stupid.
Especially if I want to replace just the third toolbutton.Isn't there a somewhat more clever way to get same result?
Just as if I want to create a toolbar that reflects function keys.
There are always 12 function keys and the keys have always the same order. So how can I reflect, that i.e. F8 has changed behaviour? -
Hi,
I worked it out :)
I replaced QToolBar and subclassed QToolButton and now I have a toolbar, where you can reference a button by position and replace the action without changing attached shortcut.
That works together with my variant of a statemachine.
See github StateMachine with branch dynamic_toolbar