Reputation: 1068
What's the correct way to apply styles for a QMenu object?
I'm trying this:
QMenu contextMenu(tr("Context menu"), this);
contextMenu.addAction(new QAction(tr("Hello"), this));
contextMenu.setStyleSheet("*:hover { color:#FFF; } *:!hover { color:#aaa; }");
I'm trying to set different text colors for when the mouse is over the menu option and when the mouse isn't over the option. But it's not working.
Upvotes: 5
Views: 12406
Reputation: 1038
In case of QMenu
styling use QMenu::item:selected
Here is an example
QMenu::item{
background-color: rgb(0, 170, 0);
color: rgb(255, 255, 255);
}
QMenu::item:selected{
background-color: rgb(0, 85, 127);
color: rgb(255, 255, 255);
}
In your case
QString menuStyle(
"QMenu::item{"
"background-color: rgb(0, 170, 0);"
"color: rgb(255, 255, 255);"
"}"
"QMenu::item:selected{"
"background-color: rgb(0, 85, 127);"
"color: rgb(255, 255, 255);"
"}"
);
this->setStyleSheet(menuStyle);
Refer the Qt Style Sheets example for more options
Upvotes: 10