The "default action" is the action you just created. Simply assign it an icon, text and whatever else you want. That's probably why you received the message in the first place - you tried to override the default action.
The "default action" is the action you just created. Simply assign it an icon, text and whatever else you want. That's probably why you received the message in the first place - you tried to override the default action.
I don't create an action, but a toolbutton!
The second test I did creating an action instead of the toolbutton did not show any more the message, but created an unwanted sub-menu.
The default action must change depending on the user clicks in the menu.
When a user clicks a menu item, the menu item's action become the default action, so that subsequent clicks on the toolbutton activate that action without using the menu.
The menu is used to change the action performed by the toolbutton click.
For this I don't want the toolbutton to have another action for himself.
Here is what the documentation of QToolButton::setDefaultAction() says:
So it seems exactly what I'm doing: I set the button's properties using the default action.void QToolButton::setDefaultAction ( QAction * action ) [slot]
Sets the default action to action.
If a tool button has a default action, the action defines the button's properties like text, icon, tool tip, etc.
Last edited by iw2nhl; 19th August 2007 at 14:55.
I meant the one in the second test. That's the "default action" for a tool button that will be created automatically by Qt when you add the action to the toolbar.
I'm not sure I understand what you want then...The default action must change depending on the user clicks in the menu.
Isn't this what you want?
Qt Code:
#include <QApplication> #include <QMainWindow> #include <QToolBar> #include <QToolButton> #include <QAction> #include <QMenu> /* Connecting POS1 and POS2 actions triggered signals to a custom slot that will hide act and show POS1 or POS2 respectively and assign it the menu will switch the visible action and retain the menu that allows to choose one of the remaining actions. */ int main(int argc, char **argv){ QMainWindow mw; act->setToolTip("Test menu"); menu->addAction("POS1"); // real actions here menu->addAction("POS2"); // real actions here act->setMenu(menu); tb->addAction(act); mw.show(); return app.exec(); }To copy to clipboard, switch view to plain text mode
Yes, it is almost what I'm doing, but the idea is a little different:
in the toolbar I want to have 2 buttons, say A (simple QToolButton) and B (QToolButton with arrow for menu) and the user 90% of the times clicks on A or B, but sometimes it wants to change what B does, so he clicks the B menu, change the action and B from now on performs the new action.
The menu of B shows all the possible assignable actions to button B, while the button himself, when clicked, performs the last selected action. Button B also changes icon/text/tooltip depending on the selected action so that the user knows how it is working.
Note that setDefaultAction() is doing exactly what I described above, just it shows the message in console every time the user clicks the arrow to show the menu of B.
In your code, in the comment you say:
"a custom slot that will hide act and show POS1 or POS2"
What do you mean with "hide act", here act is the QToolButton, how can you hide the action and show POS1 or POS2? Sorry if it's a stupid question, may be I'm not understanding something...
act is an action, not a toolbutton. It can be a menu entry as well. If you hide an action (set its visible property to false) all toolbuttons and menu entries associated with it will be hidden. The most trivial implementation of what you want is to have n actions in a toolbar (and each has the menu associated with it), but only show one of them at a time.
I'm currently looking into the sources to find out exactly why you get that message, but it'll take some time - I have to download the latest Qt source archive.
OK. Here's what happens in 4.3.1( Sorry Wysota, I promise not to answer any posts until next Saturday):
A tool button has a list of assigned actions( inherited from QWidget).
When you set the menu via setMenu:
the default(??) action of m_SelectionModeMenu will be added to m_SelectionModeButton's action list.Qt Code:
m_selectionModeButton->setMenu(m_selectionModeMenu);To copy to clipboard, switch view to plain text mode
Next, you added:
If m_actionIntersectsItem was not already the default action for the menu, it causes the tool button to add it to its action list.Qt Code:
m_selectionModeButton->setDefaultAction(m_actionIntersectsItem);To copy to clipboard, switch view to plain text mode
The message you were complaining about is outputted in QToolButton: popupTimerDone, called, for example when the menu is shown.
As you can see later in the function, if you set any menu with setMenu, then this menu will get displayed, rendering any actions that you add via addAction useless( they will be overwritten by the action you will select in the menu ).Qt Code:
if(menuAction) { actualMenu = menuAction->menu(); if (q->actions().size() > 1) qWarning("QToolButton: Menu in setMenu() overriding actions set in addAction!"); }To copy to clipboard, switch view to plain text mode
Basically the message is just a warning, so you should ignore it if you don't want to fix it.
To fix it, use:
before setting the menu to the toolbutton with setMenu. This way you add only one action to the button( the current one).Qt Code:
m_selectionModeButton->setDefaultAction(m_actionIntersectsItem);To copy to clipboard, switch view to plain text mode
Regards
Last edited by marcel; 19th August 2007 at 22:24.
If you mean this, then it also causes the warning to appear.
Qt Code:
#include <QtGui> int main(int argc, char **argv){ QMainWindow mw; menu->addAction(act1); menu->addAction(act2); menu->addAction(act3); b->setDefaultAction(act1); b->setMenu(menu); tb->addWidget(b); mw.show(); return app.exec(); }To copy to clipboard, switch view to plain text mode
I'd ignore the "default action" and do the thing manually or just turn off debugging messages![]()
Thank you marcel for your detailed explanation, it was very helpful!
I could solve the problem!
Although your solution did not work (changing the position of setDefaultAction() did not change anything, moreover I still needed to call it in the slots at user request), this sentence made me understand the problem:
As you told, every QWidget has a list of actions, so adding a QMenu to it was not necessary and went to override the internal list of actions. The list was not empty because of setDefaultAction() which added the action to the list of widget actions in some way.
The solutions now is very simple: instead of adding the actions to a QMenu and setting the menu to the QToolButton, you need only to add the actions directly to the QToolButton!
In this way the menu is auto-generated with the list of the QWidget actions.
Here is the code:
Now the code is even simpler!Qt Code:
m_selectionModeButton->addAction(m_actionIntersectsItem); // New line! m_selectionModeButton->addAction(m_actionContainsItem); // New line! m_selectionModeButton->setDefaultAction(m_actionIntersectsItem); // Removed lines //m_selectionModeMenu = new QMenu; //m_selectionModeMenu->addAction(m_actionIntersectsItem); //m_selectionModeMenu->addAction(m_actionContainsItem); //m_selectionModeButton->setMenu(m_selectionModeMenu);To copy to clipboard, switch view to plain text mode
I hope this solution can help someone else too in the future!
Thank you very much to every one for the help and thank you again marcel for the problem description which made me find the solution!
Well, I didn't test it... Seemed like it would work at the moment.Although your solution did not work (changing the position of setDefaultAction() did not change anything,
Regards
The solutions now is very simple: instead of adding the actions to a QMenu and setting the menu to the QToolButton, you need only to add the actions directly to the QToolButton!
This is not always when one has to create a separate menu not only the action. One example of such case is when someone has to enable the tearing off on menu using "setTearOffEnabled(true)".
So adding the actions to QMenu and setting the menu to QToolButton is needed int his case. How to overcome the problem here?
Bookmarks