我有一个带圆角的QPushButton:
现在,我想在鼠标移动时更改按钮的样式:
我使用了QPushButton的setStyleSheet:
QPushButton{
width:100px;
height:80px;
border-radius:40px;
border: 1px solid #ffffff;
}
QPushButton:hover{
background: red;
}
但不是四个角落:
作为显示的第3张图像,光标指向按钮的右下角,但也触发了按钮的悬停状态。有没有一种简单的方法可以防止这种行为?
也许,存在一个更直接的解决方案,但我只是尝试了,并且发生了手动实现所有悬停行为似乎唯一真正有效的方式。
通常,所有关于检查鼠标坐标是否属于椭圆(您可以尝试其他几何考虑因素)。
有些东西不起作用:
ui->myButton->installEventFilter(this);
//----------------------------------------
bool SomeWidget::eventFilter(QObject *obj, QEvent *event)
{
if (event->type() == QEvent::HoverEnter || event->type() == QEvent::HoverMove)
{
QHoverEvent* hoverEvent = static_cast<QHoverEvent *>(event);
QWidget* target = qobject_cast<QWidget *>(obj);
qreal a = target->width() / 2;
qreal b = target->height() / 2;
qreal x = hoverEvent->posF().x() - a;
qreal y = hoverEvent->posF().y() - b;
double val = (x / a) * (x / a) + (y / b) * (y / b);
bool isInside = val <= 1;
if (!isInside)
return true;
else
return QObject::eventFilter(obj, event);
}
return QObject::eventFilter(obj, event);
}
使用一些Widget,包含一个按钮和事件过滤器安装,这看起来很有前景,但工作得非常好=(
可以作为概念验证的东西:
CustomButton::CustomButton(QWidget *parent)
: QPushButton(parent)
{
setProperty("my_hover", "not_hovered");
}
void CustomButton::mouseMoveEvent(QMouseEvent *event)
{
qreal a = width() / 2;
qreal b = height() / 2;
qreal x = event->pos().x() - a;
qreal y = event->pos().y() - b;
double val = (x / a) * (x / a) + (y / b) * (y / b);
bool isInside = val <= 1;
setProperty("my_hover", isInside ? "hovered" : "not_hovered" );
style()->unpolish(this);
style()->polish(this);
}
对QPushButton进行子类化工作效果更好(上面的代码是一个可读的样本,当然可以改进它)。实现时,它为按钮类提供了一个自定义属性,可以这样使用:
QPushButton[my_hover="hovered"]
{
background: red;
}
为了使它成为一个完美的解决方案,你可能也必须关心mouseLeaveEvent
,从而摆脱偶尔的故障。此外,如果它与你的Qt一起工作,那么该属性可能会被设为boolean。
AFAIK,这是实现您所描述的所需行为的唯一可靠方法。