Hi,
menu.addAction("Item 1");
menu.addAction("Item 2");
menu.addAction("Item 3");
qApp->setStyleSheet("QMenu::item:selected { background: rgba(0, 0, 128, 20%); }");
QMenu menu;
menu.addAction("Item 1");
menu.addAction("Item 2");
menu.addAction("Item 3");
qApp->setStyleSheet("QMenu::item:selected { background: rgba(0, 0, 128, 20%); }");
To copy to clipboard, switch view to plain text mode
This example creates a simple QMenu where the backgrounds of the single items get colored when the mouse is hovering over them. This also works if the mouse is pressed on "Item 1" and then hovered over the other ones (not releasing the mouse button).
Is there a way to achieve this behaviour on widgets in a layout?
layout.
addWidget(new QLabel("Item 1"));
layout.
addWidget(new QLabel("Item 2"));
layout.
addWidget(new QLabel("Item 3"));
qApp->setStyleSheet("QLabel:hover { background: rgba(0, 0, 128, 20%); }");
QVBoxLayout layout;
layout.addWidget(new QLabel("Item 1"));
layout.addWidget(new QLabel("Item 2"));
layout.addWidget(new QLabel("Item 3"));
qApp->setStyleSheet("QLabel:hover { background: rgba(0, 0, 128, 20%); }");
To copy to clipboard, switch view to plain text mode
With this example, the labels only get colored when the mouse is hovering them without a mouse button being pressed. If I press and hold the left mouse button on "Item 1" on hover over the other items, "Item 1" remains being colored. The others will not get colored.
I guess this is a problem of event propagation. When the mouse is pressed and moved, only "Item 1" receives events (QEvent::MouseMove and QEvent::HoverMove). The others are not receiving any type of event.
So is there a way to solve this problem, i.e. to let the other items also receive the events? Or if it is not a problem of event propagation, how do I achieve the same behaviour like on the QMenu?
Thanks in advance for your help.
Bookmarks