Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QT: set stylesheet for a QMenu object

Tags:

qt

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.

like image 745
Stephen H. Anderson Avatar asked Jul 25 '15 12:07

Stephen H. Anderson


1 Answers

  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

like image 185
techneaz Avatar answered Oct 24 '22 04:10

techneaz