使用 Hover 和 Pressed 样式表技术的 Qt

24

我在我的按钮pushButton样式表中使用了这个

 QPushButton#pushButton {
     background-color: yellow;
 }
 QPushButton#pushButton:pressed {
     background-color: rgb(224, 0, 0);     
 }
 QPushButton#pushButton:hover {
     background-color: rgb(224, 255, 0);
 }
当我将鼠标悬停在上面时,它会改变颜色,就像我期望的那样,但是当我按下按钮后,悬停颜色仍然存在。 我尝试改变顺序,但问题仍旧存在。 对Qt还不太熟悉。

4
按压按钮。啊呜! - Michael Scheper
4个回答

52

好的! 运行良好。 非常感谢。 我使用了这个: QPushButton#pushButton { background-color:red }QPushButton#pushButton:hover:!pressed{ background-color:green} QPushButton#pushButton:hover { background-color:yellow } - harveyslash
这是对你问题的回答,但只有一行。如果你不理解,我可以进行调整;主要思想是你可以随意组合状态。我建议阅读qt文档,里面有很多有用的示例 - http://doc.qt.digia.com/4.7/stylesheet-reference.html - Dmitry Sazonov

5

Css和Qt CSS都依赖于声明的顺序。具有相同特定性的后续声明将覆盖先前声明。因此,为了使pressed状态优先于其他状态,只需将其放在hover状态之后即可。

QPushButton#pushButton {
    background-color: yellow;
}

QPushButton#pushButton:hover {
    background-color: rgb(224, 255, 0);
}

QPushButton#pushButton:pressed {
    background-color: rgb(224, 0, 0);     
}

在Windows 10上,只有在单击后才能正常工作,无法在悬停时工作,这是正常的吗? - SamTheProgrammer

2
这是您想要的正确样式表:

这是正确的样式表:

//base stylesheet
QPushButton
{
   background-color: yellow;
}
//pressed button stylesheet
QPushButton:pressed
{
  background-color: rgb(224, 0, 0);     
}
//hover stylesheet
QPushButton:hover:!pressed
{
  background-color: rgb(224, 255, 0);
}

-2

您可以在QPushButton中设置图像:

QPushButton#pushButton {
     background-url(Images/image1.png);
 }
 QPushButton#pushButton:pressed {
     background-url(Images/image2.png);   
 }

 QPushButton#pushButton:hover {
      background-url(Images/image3.png);
 }

17
你为什么认为这个问题是关于背景图片的? - Michael Scheper

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接