Remove hover state from QPushButton



  • I have a custom stylesheet that applies an orange-ish color to my hover buttons.

    So, I have this button that I use in order to show my application's menu (something like what Chrome does to show its menu, on the upper right corner).

    The problem is that once I click the menu button and then I click somewhere else (and thus the menu closes), the hover color of the menu button remains. I have to re-hover and then hover out of the button to make it change back to the original color.

    While I find it a good idea to stay at the hover color as long as the menu is open, it is totally wrong to remain this color when the menu closes.

    I tried to manually send a hover out event to the push button when the menu closes, but that doesn't work.

    This is what I tried:

    @connect(settingsMenu_, SIGNAL(aboutToHide()), this, SLOT(unhoverMenuButton()));

    ...

    void MainWindow::unhoverMenuButton(){
    QMouseEvent *event1 = new QMouseEvent(QEvent::MouseMove, QCursor::pos()+QPoint(1, 1), Qt::NoButton, Qt::NoButton, Qt::NoModifier);
    QApplication::postEvent(ui->settingsMenuButton_, event1, Qt::HighEventPriority);
    }@

    I also tried with

    @QMouseEvent *event2 = new QMouseEvent(QEvent::HoverLeave, QCursor::pos(), QCursor::pos()+QPoint(1, 1));@

    All of the above to no avail. The only thing that worked was to set the whole button to disabled state and have a singleshot timer to set it back to enabled state shortly, but that's way too dirty for me to have in production code.



  • you may return the color "standard" when you release the button.



  • I know this is an old thread, but did you ever get an answer to this? I have no idea what Salvatello is talking about.

    I have a similar issue when I dock tool bars and the first button in the toolbar remains with a hover style. It is easily demonstrated using the mainwindows example delivered with Qt just by dragging the tool bas around a bit.

    It appears to be that animations do not take account of relative mouse movements during the animation, but how I explicitly change state and force a redraw I don't know.



  • I found the solution for my case so I'm posting it for anyone else that comes across this thread.

    The problem is that underMouse() does not return the correct result after a drag drop operation. In QWidget.cpp there is actually a comment to this effect (line 2893 in Qt 5.5).

    Since this function is used to determine the hover state you have to explicitly reset it on the control that is displaying hover state incorrectly.

    In my case the time to do that is to handle the QToolBar::topLevelChanged signal and call setAttribute(Qt::WA_UnderMouse, false); on the first widget of the toolbar. The OP would need it in the unhoverMenuButton function.

    
    // Slot called when bar changes from floating to docked and vice versa
    void MyToolBar::OnTopLevelChanged(bool top_level)
    {
        if (!top_level)
        {
            // Tool bar has been docked.
            // Get a pointer to the first widget on the tool bar
            // which in my case is saved when I add it and
            // reset the WA_UnderMouse flag.
            pFirstWidget->setAttribute(Qt::WA_UnderMouse, false);
        }
    }
    
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.