title: highlighting custom QWidgetAction on hover
body: <p>My application has a <code>QMenuBar</code> with a number of <code>QMenu</code>s, each of which having a number of <code>QAction</code>s and sub-<code>QMenu</code>s.
  Most of the <code>QAction</code>-items are derivatives of <code>QWidgetAction</code> with re-implemented <code>QWidgetAction::createWidget</code> methods.</p>
  
  <p>Usually, both <code>QAction</code>s and <code>QMenu</code> become highlighted on mouse hover.
  Even a <code>QWidgetAction</code> doesn't make trouble until here:</p>
  
  <p><a href="https://i.stack.imgur.com/8Oapq.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/8Oapq.gif" alt="animated graphics of how highlighting is expected to work"></a></p>
  
  <p>But as soon as I override <code>QWidgetAction::createWidget</code> to return a custom <code>QWidget</code></p>
  
  <pre><code>QWidget* MyWidgetAction::createWidget(QWidget* parent) { return new MyWidget(parent); }
  </code></pre>
  
  <p>the highlighting does not work anymore. So I implemented it myself:</p>
  
  <pre><code>void MyWidget::set_highlighted(bool h)
  {
    setBackgroundRole(h ? QPalette::Highlight : QPalette::Window);
    setAutoFillBackground(h);
  }
  void MyWidget::enterEvent(QEvent*) override { set_highlighted(true); }
  void MyWidget::leaveEvent(QEvent*) override { set_highlighted(false); }
  </code></pre>
  
  <p>However, it does not behave as expected:</p>
  
  <p><a href="https://i.stack.imgur.com/aYeRX.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/aYeRX.gif" alt="animated graphics of what goes wrong with highlighting"></a></p>
  
  <p>I already figured out that the <code>enterEvent</code> method is not called until all sub-menus are closed, which only happens with some delay after mouse leaves the sub menu or its action (btw, how can I change the delay?). Same with mouse-move events.</p>
  
  <p><em>Question</em>: How can I re-implement highlight-on-hover properly? The user shall not notice that custom widget and standard <code>QAction</code> behave differently.
  What does the default <code>QWidgetAction::createWidget</code> do and how can I reproduce it? I've already looked at <a href="https://code.woboq.org/qt5/qtbase/src/widgets/kernel/qwidgetaction.cpp.html#_ZN13QWidgetAction12createWidgetEP7QWidget" rel="noreferrer">Qt's source</a> but it's quite confusing.</p>
  
  <p><a href="https://gist.github.com/pasbi/336a75248e1fee2ece7c77a24babfe99" rel="noreferrer">Code to reproduce the animations</a></p>
  
  <p><a href="https://github.com/pasbi/ommpfritt" rel="noreferrer">Actual production code</a></p>