QT:为QMenu对象设置样式表

Ste*_*son 4 qt

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

我正在尝试这个:

QMenu contextMenu(tr("Context menu"), this);
contextMenu.addAction(new QAction(tr("Hello"), this));
contextMenu.setStyleSheet("*:hover { color:#FFF; } *:!hover { color:#aaa; }");
Run Code Online (Sandbox Code Playgroud)

我正在尝试设置不同的文本颜色,当鼠标位于菜单选项上时以及鼠标未超出选项时.但它不起作用.

tec*_*eaz 6

  1. 如果QMenu使用造型QMenu::item:selected

  2. 这是一个例子

     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);
     } 
    
    Run Code Online (Sandbox Code Playgroud)
  3. 在你的情况下

      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);
    
    Run Code Online (Sandbox Code Playgroud)
  4. 有关更多选项,请参阅Qt样式表示例