QT:为QMenu对象设置样式表

QT:为QMenu对象设置样式表,qt,Qt,为QMenu对象应用样式的正确方法是什么 我正在尝试这个: QMenu contextMenu(tr("Context menu"), this); contextMenu.addAction(new QAction(tr("Hello"), this)); contextMenu.setStyleSheet("*:hover { color:#FFF; } *:!hover { color:#aaa; }"); 我正在尝试为鼠标位于菜单选项上方和鼠标未位于菜单选项上方时设置不同的文本颜色。但

为QMenu对象应用样式的正确方法是什么

我正在尝试这个:

QMenu contextMenu(tr("Context menu"), this);
contextMenu.addAction(new QAction(tr("Hello"), this));
contextMenu.setStyleSheet("*:hover { color:#FFF; } *:!hover { color:#aaa; }");
我正在尝试为鼠标位于菜单选项上方和鼠标未位于菜单选项上方时设置不同的文本颜色。但它不起作用

  • 如果是
    QMenu
    样式设置,请使用
    QMenu::item:selected

  • 这里有一个例子

     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);
     } 
    
  • 就你而言

      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);
    
  • 有关更多选项,请参阅