سؤال

I have a QWidgetAction with three QPushButtons and one QMenu with many actions, including one's with sub-menus. This menu looks like on the picture below, where blue line is separator.

So, when I'm trying to hover one of three buttons with mouse from menu item which have sub-menu, I have no hover effect which set from css. But when I'm trying to hover from menu item which don't have any sub-menus, all works fine and I have colored background.

So, questions is: why this works in such way and how to fix it?

I already tried to play with code and, yeah, the behavior of control is changed, but not the way I need(I got button colored but with wrong color and shape):

bool CutCopyPasteWidget::eventFilter(QObject* object, QEvent* event)
{
if (object == m_cutButton || object == m_copyButton || object == m_pasteButton)
{
    QEvent::Type t = event->type();

    if (t == QEvent::Enter)
    {
        QPushButton* w = qobject_cast<QPushButton*>(object);
        w->setAttribute(Qt::WA_Hover);
        w->setAutoFillBackground(true);
        w->update();
        emit entered();
    }
    else if (t == QEvent::Leave)
    {
        QPushButton* w = qobject_cast<QPushButton*>(object);
        w->setAttribute(Qt::WA_Hover, false);
        w->setAutoFillBackground(false);
        w->update();
    }
}

return QWidget::eventFilter(object, event);
}

Style:

CutCopyPasteWidget QPushButton:hover:enabled
{
background: HOVERED_CONTEXT_BUTTON_GRADIENT;
border-color: HOVERED_ACTIVE_CONTEXT_BUTTON_BORDER;
}

CutCopyPasteWidget QPushButton:!hover:enabled
{
background: none;
border-color: ACTIVE_CONTEXT_BUTTON_BORDER;
}

CutCopyPasteWidget QPushButton:!hover:!enabled
{
background: none;
border-color: INACTIVE_CONTEXT_BUTTON_BORDER;
}

CutCopyPasteWidget QPushButton
{
min-width: 2pt; 
}

example of menu

UPDATE: I have created a github repo with example to demonstrate the problem.

هل كانت مفيدة؟

المحلول

Well, I didn't find any explanations or suggestions, so I have found solution by myself.

To highlight button in QWidgetAction, I added dynamic property, like in the code below in a place where buttons were constucted:

m_pasteButton->setProperty("mouseHover", false);

Then, In installed filters on the buttons and catch Enter and Leave events to figure out when the mouse is hovered or not:

bool CustomWidgetAction::eventFilter(QObject* object, QEvent* event)
{
    if (object == m_cutButton)
    {
        QEvent::Type t = event->type();
        if (t == QEvent::Enter)
        {
            QWidget* w = qobject_cast<QWidget*>(object);
            w->setProperty("mouseHover", true);
            w->polish(w);
        }
        else if (t == QEvent::Leave)
        {
            QWidget* w = qobject_cast<QWidget*>(object);
            w->setProperty("mouseHover", false);
            w->polish(w);
        }
    }
    return QWidget::eventFilter(object, event);
}

And then, in the css, I added some rules to set hover effect:

CustomWidgetAction QPushButton[mouseHover="true"]:enabled
{
    background: HOVERED_CONTEXT_BUTTON_GRADIENT;
    border-color: HOVERED_ACTIVE_CONTEXT_BUTTON_BORDER;
}

CustomWidgetAction QPushButton[mouseHover="false"]:enabled
{
    background: none;
    border-color: ACTIVE_CONTEXT_BUTTON_BORDER;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top