My application has a QMenuBar
with a number of QMenu
s, each of which having a number of QAction
s and sub-QMenu
s.
Most of the QAction
-items are derivatives of QWidgetAction
with re-implemented QWidgetAction::createWidget
methods.
Usually, both QAction
s and QMenu
become highlighted on mouse hover.
Even a QWidgetAction
doesn't make trouble until here:
But as soon as I override QWidgetAction::createWidget
to return a custom QWidget
QWidget* MyWidgetAction::createWidget(QWidget* parent) { return new MyWidget(parent); }
the highlighting does not work anymore. So I implemented it myself:
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); }
However, it does not behave as expected:
I already figured out that the enterEvent
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.
Question: How can I re-implement highlight-on-hover properly? The user shall not notice that custom widget and standard QAction
behave differently.
What does the default QWidgetAction::createWidget
do and how can I reproduce it? I've already looked at Qt's source but it's quite confusing.
MyWidget
overrideenterEvent
andleaveEvent
already or just as part of your attempt to solve the issue? – Rasp