Qt QPushButton 样式表悬停 [英] Qt QPushButton stylesheet hover

查看:111
本文介绍了Qt QPushButton 样式表悬停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下按钮样式表:

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;
}

我有一个按钮.当它被按下时,一个侧边小部件被调整大小.因此按钮获得按下样式,当我释放它时它获得悬停样式.此外,小部件被调整大小,按钮跟随"小部件.问题是当我用鼠标做一些移动时,按钮保持悬停状态并失去它.这是来自 qt 的错误还是我遗漏了样式表代码中的某些内容.

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.

我做了一个 gif 动画来展示这种情况:

I did a animated gif showing the situation:

谢谢

推荐答案

你可以说这是 Qt 中的一个 bug.我会说这是一种由正确逻辑引起的错误.自己判断吧.Hover 状态由 WA_UnderMouse 小部件属性定义.此属性由应用程序设置:

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::EnterQEvent::Leave 事件仅在应用程序从操作系统接收鼠标事件时发送.
您不移动鼠标,应用程序不会收到任何鼠标事件,因此 WA_UnderMouse 属性不会更改.
解决此问题的方法之一是在移动按钮小部件时自行将 Qt::WA_UnderMouse 属性设置为正确的值.

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.

这篇关于Qt QPushButton 样式表悬停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆