Stephen H. Anderson
Stephen H. Anderson

Reputation: 1068

QT: set stylesheet for a QMenu object

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

Answers (1)

techneaz
techneaz

Reputation: 1038

  1. In case of QMenu styling use QMenu::item:selected

  2. 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);
     } 
    
  3. 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);
    
  4. Refer the Qt Style Sheets example for more options

Upvotes: 10

Related Questions