title: QMenu: How to customize the menu items of QMenu
body: <p>I want to build a dropdown list control with QPushButton and QMenu like below:</p>
  
  <pre><code>QPushButton* menuBt = new QPushButton("Please select");
  menuBt-&gt;setFlat(true);
  QMenu* menu = new QMenu();
  menuBt-&gt;setMenu(menu);
  QWidgetAction* wa1 = new QWidgetAction(menu);
  QLabel* l1 = new QLabel("Option1");
  wa1-&gt;setDefaultWidget(l1);
  menu-&gt;addAction(wa1);
  QWidgetAction* wa2 = new QWidgetAction(menu);
  QLabel* l2 = new QLabel("Option2");
  wa2-&gt;setDefaultWidget(l2);
  menu-&gt;addAction(wa2);
  menu-&gt;setStyleSheet("QMenu::item {font-family: \"Arial\"; font-size: 13pt; color: #808080; border: 1px solid gray; background-color: rgb(234,234,234);}"
      "QMenu::item:hover {background-color: rgb(0, 0, 255);}");
  menuBt-&gt;setStyleSheet("QPushButton {font-family: \"Arial\"; font-size: 13pt; color: #808080; border: 1px solid gray;padding: 1px 18px 1px 3px;min-width: 6em; background-color: rgb(234,234,234);}");
  </code></pre>
  
  <p>I set the font and hover background color to the menu items by setStyleSheet, but seems it doesn't work.
  How make the font and hover background color work on menu items?</p>
  
  <p>Answer:</p>
  
  <pre><code>class QTDropDownButton : public QPushButton
  {
      Q_OBJECT
  public:
      QTDropDownButton(QString text, QWidget *parent = nullptr);
  
      void addItem(QString text);
  
      protected slots:
          void menuAboutToShow();
  
  private:
      QMenu* menu_;
  };
  
      QTDropDownButton::QTDropDownButton(QString text, QWidget *parent) :
      QPushButton(text, parent)
  {
      setFlat(true);
      menu_ = new QMenu();
      setMenu(menu_);
  
      connect(menu_, SIGNAL(aboutToShow()), this, SLOT(menuAboutToShow()));
  
      setStyleSheet("font-family: Arial; font-size: 13pt; color: #808080; border: 1px solid gray; background-color: rgb(234,234,234);");
      menu_-&gt;setStyleSheet("QMenu::item {font-family: Arial; font-size: 13pt; color: #808080; border: 1px solid gray; background-color: rgb(234,234,234);}"
          "QMenu::item:selected {background-color: rgb(0, 255, 255);}"
          "QLabel {font-family: Arial; font-size: 13pt;}"
          "QLabel:hover {background-color: rgb(0, 0, 255);}");
  }
  
  void QTDropDownButton::addItem(QString text)
  {
      if(!menu_)
          return;
  
      QWidgetAction* wa1 = new QWidgetAction(menu_);
      QLabel* l1 = new QLabel(text);
      wa1-&gt;setDefaultWidget(l1);
      menu_-&gt;addAction(wa1);
  }
  
  void QTDropDownButton::menuAboutToShow()
  {
      if(menu_)
          menu_-&gt;setFixedWidth(this-&gt;width());
  }
  </code></pre>