[英]QT custom widget hover
i made custom widget with two labels inside.我制作了带有两个标签的自定义小部件。 First one is for showing chosen color and second one is just for displaing text.第一个用于显示选择的颜色,第二个用于显示文本。 I want to make hover effect.我想制作悬停效果。 I tried different approaches but all of them ends with the same result.我尝试了不同的方法,但所有方法都以相同的结果结束。 Hover works but only for those two block seperately (like in the pictures below:)悬停有效,但仅适用于这两个块(如下图所示:)
I want to highlight whole block as one (the red rectangle area showed below):我想将整个块突出显示为一个(如下所示的红色矩形区域):
I tried to achieve that inside of custom widget constructor with:我试图在自定义小部件构造函数中实现:
this->setStyleSheet(":hover{ background-color: rgba(0,20,100,0.5); }");
Also i tried to add QFrame object to my ui and replace that with my custom widget.我还尝试将 QFrame 对象添加到我的 ui 并用我的自定义小部件替换它。 Both methods are not working.这两种方法都不起作用。 I found that topic: Set a StyleSheet for a whole widget in Qt我找到了这个话题: 在 Qt 中为整个小部件设置样式表
where someone managed to make it with:有人设法做到了:
Finally I solved the problem creating a QFrame inside the main QWidget and setting the StyleSheet of that QFrame.最后我解决了在主 QWidget 中创建 QFrame 并设置该 QFrame 的 StyleSheet 的问题。
I tried that approach but i'm not sure if i applied that correctly (propably not since that's still not working).我尝试过这种方法,但我不确定我是否正确地应用了它(可能不是因为那仍然不起作用)。 Can someone help me with that?有人可以帮我吗?
I has a same problem, i guess your solution is simplest.我有同样的问题,我想你的解决方案是最简单的。 btw the right decision is overrided EventFilter() method for your custom widget.顺便说一句,正确的决定是为您的自定义小部件覆盖 EventFilter() 方法。 like this:像这样:
bool CustomWidget::eventFilter(QObject *obj, QEvent *event)
{
if (obj == ui->pushButton) {
QEvent::Type type = event->type();
if (type == QEvent::HoverLeave) {
qDebug()<<"No";
} else if (type == QEvent::HoverEnter) {
qDebug()<<"YES";
}else if (type == QEvent::MouseButtonPress) {
qDebug()<<"enter";
}
}
return QWidget::eventFilter(obj, event);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.