使用悬停和按下的样式表Qt

har*_*ash 17 qt rollover stylesheet hover

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

 QPushButton#pushButton {
     background-color: yellow;
 }
 QPushButton#pushButton:pressed {
     background-color: rgb(224, 0, 0);     
 }
 QPushButton#pushButton:hover {
     background-color: rgb(224, 255, 0);
 }
Run Code Online (Sandbox Code Playgroud)

当我将鼠标悬停在它上面时,它会改变颜色,就像我期望的那样,但是当我按下按钮时,悬停颜色仍然存在.我尝试改变顺序,但它仍然是同样的问题.Qt中的新鲜事.

Dmi*_*nov 34

您可以组合状态,例如:

QPushButton:hover:!pressed
{
  border: 1px solid red;
}
Run Code Online (Sandbox Code Playgroud)

QSS参考 - 状态


mfl*_*din 5

CSS 和 QSS(Qt 样式表)取决于声明的顺序。具有相同特性的后续声明将覆盖先前的声明。因此,为了让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);     
}
Run Code Online (Sandbox Code Playgroud)