
Originally Posted by
d_stranz
Of course, if you implement this, you'll be sending all the wrong UI clues to the user. Pressing the mouse on one button, moving to another, then releasing the mouse on a different button should not result in the same behavior as an actual click on the second button. No other GUI around has that behavior. Menus are different - people understand this - when you press the mouse on a top level item then move down the list, they know that whatever item they release it on is the one that will be executed. If you want to cancel a menu action, you move off the menu. Same thing with buttons. If you accidentally move off one button and release the mouse over another, a user never expects that to behave the same way as a click on the second button.
I have to contradict here. A QMenu does behave differently. You do not have to press and hold the mouse button on a top level item.
menu.addAction("Item 1", this, SLOT(action1()));
menu.addAction("Item 2", this, SLOT(action2()));
menu.addAction("Item 3", this, SLOT(action3()));
qApp->setStyleSheet("QMenu::item:selected { background: rgba(0, 0, 128, 20%); }");
QMenu menu;
menu.addAction("Item 1", this, SLOT(action1()));
menu.addAction("Item 2", this, SLOT(action2()));
menu.addAction("Item 3", this, SLOT(action3()));
qApp->setStyleSheet("QMenu::item:selected { background: rgba(0, 0, 128, 20%); }");
To copy to clipboard, switch view to plain text mode
If this QMenu is open and you press the mouse button on "Item 1", move the mouse over to "Item 3" and release it there, "action3()" will be executed. There is no need to hold the button down on the top level item. You can simply open the menu, release the button that opened it, then press and hold the button on any item in the menu and release it over another. The QAction on which the button was released will be executed.
The background coloring is done through Qt like expected, i.e. the item over which the mouse resides is colored, regardless whether the mouse button is pressed and hold or not.
This is the default behaviour in many GUIs and Qt also does it.
To return to my question: Implement it like this
action1
->setDefaultWidget
(new QLabel("Item 1"));
action2
->setDefaultWidget
(new QLabel("Item 2"));
action3
->setDefaultWidget
(new QLabel("Item 3"));
menu.addAction(action1);
menu.addAction(action2);
menu.addAction(action3);
qApp->setStyleSheet("QMenu::item:selected, *:hover { background: rgba(0, 0, 128, 20%); }");
QWidgetAction * action1 = new QWidgetAction(this);
QWidgetAction * action2 = new QWidgetAction(this);
QWidgetAction * action3 = new QWidgetAction(this);
action1->setDefaultWidget(new QLabel("Item 1"));
action2->setDefaultWidget(new QLabel("Item 2"));
action3->setDefaultWidget(new QLabel("Item 3"));
QMenu menu;
menu.addAction(action1);
menu.addAction(action2);
menu.addAction(action3);
qApp->setStyleSheet("QMenu::item:selected, *:hover { background: rgba(0, 0, 128, 20%); }");
To copy to clipboard, switch view to plain text mode
The click behaviour remains the same, i.e. press and hold the mouse on "Item 1", move it over to "Item 3" and release it there, results in "action3" being executed.
BUT the background coloring behaves differently. In this case, the background of "Item 1" remains colored regardless where the mouse cursor resides.
So the user gets the visual feedback of "Item 1" being colored even if the mouse cursor is over "Item 3", but if he releases the mouse button over "Item 3" "action3" gets executed.
I suspect, that this is a problem of mouse grabbing and event propagation, but am not sure.
The question is, how can I give the user the same visual feedback when using QWidgetActions?
Bookmarks