Qt QPushButton stylesheet hover

不打扰是莪最后的温柔 提交于 2019-12-23 19:28:32

问题


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:

Thanks


回答1:


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.



来源:https://stackoverflow.com/questions/25318538/qt-qpushbutton-stylesheet-hover

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!