24

I used this in my button pushButton stylesheet

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

when I hover my mouse over it, it changes color, like I expect it to , But the hover color remains even when I press the button. I tried changing the order, but its still the same problem . little new in Qt.

mitjap
  • 495
  • 1
  • 6
  • 14
harveyslash
  • 5,906
  • 12
  • 58
  • 111

4 Answers4

52

You can combine states, for example:

QPushButton:hover:!pressed
{
  border: 1px solid red;
}

QSS reference - states

Dmitry Sazonov
  • 8,801
  • 1
  • 35
  • 61
  • okay ! works fine. thanks a lot I used this :: QPushButton#pushButton { background-color:red } QPushButton#pushButton:hover:!pressed{ background-color:green } QPushButton#pushButton:hover { background-color:yellow } – harveyslash Oct 03 '13 at 14:42
  • It is an answer to your question, but in one line. I'll adjust it, if you don't understand ;). Main idea, that you can combine states as you wish. I propose to read qt documentation, there are a lot of useful samples - http://doc.qt.digia.com/4.7/stylesheet-reference.html – Dmitry Sazonov Oct 03 '13 at 14:43
5

Css, and Qt CSS, depends on the order of declarations. Later declarations with the same specificity will overwrite previous declarations. So, in order to have the pressed state take precedence, simply move it below the hover state.

QPushButton#pushButton {
    background-color: yellow;
}

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

QPushButton#pushButton:pressed {
    background-color: rgb(224, 0, 0);     
}
mflodin
  • 1,093
  • 1
  • 12
  • 22
2

This is the correct stylesheet as you want:

//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

You can set the image in QPushButton:

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

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