需要设置样式:
"QMenu::item:selected[itemColor = false]{color:#E8E8E8;border:0px solid #575757;#background:transparent;}"
"QMenu::item:selected[itemColor = true]{color:#E8E8E8;border:0px solid #575757;#background:#1c1c1c;}"
事件触发的时候设置对应的属性显示到指定样式,这样的方式比直接调用qss效率高,但是比较繁琐。
connect(m_menu, &Qmenu::hovered, [&][QAction *action]){
if(action == m_exitAction)
{
m_menu->style()->unpolish(m_menu);
m_menu->style()->setProperty("itemColor", true);
m_menu->style()->polish(m_menu);
}
else
{
m_menu->style()->unpolish(m_menu);
m_menu->style()->setProperty("itemColor", false);
m_menu->style()->polish(m_menu);
}
});
参考:https://forum.qt.io/topic/81644/stylesheet-recomputing-after-property-change