Search code examples
qtstylesheetqtstylesheets

Qt QPushButton stylesheet hover


I have the following pushbutton stylesheet:

QPushButton:hover{
        background: qlineargradient(x1 : 0, y1 : 0, x2 : 0, y2 :   1, stop :   0.0 #ffd9aa,
                stop :   0.5 #ffbb6e, stop :   0.55 #feae42, stop :   1.0 #fedb74);
}

QPushButton {
        border: 1px solid #6593cf;
        border-radius: 2px;
        padding: 5px 15px 2px 5px;
        background: qlineargradient(x1 : 0, y1 : 0, x2 : 0, y2 :   1, stop :   0.0 #f5f9ff,
                stop :   0.5 #c7dfff, stop :   0.55 #afd2ff, stop :   1.0 #c0dbff);
        color: #006aff;
        font: bold large "Arial";
        height: 30px;
}




QPushButton:pressed {
        background: qlineargradient(x1 : 0, y1 : 0, x2 : 0, y2 :   1, stop :   0.0 #c0dbff,
        stop :   0.5 #cfd26f, stop :   0.55 #c7df6f, stop :   1.0 #f5f9ff);
        padding-top: 2px;
        padding-left: 3px;

}


QPushButton:on {
        background: qlineargradient(x1 : 0, y1 : 0, x2 : 0, y2 :   1, stop :   0.0 #5AA72D,
        stop :   0.5 #B3E296, stop :   0.55 #B3E296, stop :   1.0 #f5f9ff);
        padding-top: 2px;
        padding-left: 3px;
}

QPushButton:disabled {
        background: transparent #e5e9ee;
        padding-top: 2px;        
        padding-left: 3px;
        color: black;
}

I have a button. When it's pressed a side widget is resized. Thus the button gets the pressed style, when I release it gets the hover style. Furthermore the widget is resized and the button "follows" the widget. The problem is that the button keeps the hover state and loses it when I do some movement with the mouse. Is this a bug from qt or I miss something in the stylesheet code.

I did a animated gif showing the situation: enter image description here

Thanks


Solution

  • You can say that it's a bug in Qt. I would say it's a kind of bugs caused by right logic. Judge for yourself. Hover state is defined by WA_UnderMouse widget attribute. This attribute is set by the application:

    if ((e->type() == QEvent::Enter || e->type() == QEvent::DragEnter) ...
            widget->setAttribute(Qt::WA_UnderMouse, true);
    else if (e->type() == QEvent::Leave || e->type() == QEvent::DragLeave)
            widget->setAttribute(Qt::WA_UnderMouse, false);
    

    QEvent::Enter and QEvent::Leave events are only sent when the application receives mouse event from OS.
    You don't move mouse, the application doesn't receive any mouse event and so WA_UnderMouse attribute is not changed.
    One of the ways to fix that is to set Qt::WA_UnderMouse attribute to the right value by yourself when moving the button widget.