3

I am learning to use the Qt Stylesheet to add different styles to my application. I looked online and saw the Qt Documentation which said that you can use something called ID Selector that can apply the theme to certain objects. This is how I implemented this feature :

QPushButton#button
{
    color:red;
}

But it doesn't work. It only works when I try without the ID Selector:

QPushButton
{
    color:red;
}

Why doesn't this work ? I used copy and paste, so I'm 100% sure that I didn't got the name wrong.

Hung Truong
  • 401
  • 1
  • 3
  • 12
  • Did you [`yourButton->setObjectName(yourID);`](http://doc.qt.io/qt-5/qobject.html#objectName-prop) the target QPushButton ? – O'Neil May 04 '17 at 02:52

2 Answers2

5

I found out the answer, and it's actually really simple. I forgot to add button->setObjectName("Name");, which caused the stylesheet to not know about the button. Just add in this line of code and it should work properly.

Hung Truong
  • 401
  • 1
  • 3
  • 12
0

which version? please tell me and sent the whole code. because 4.7 4.8 5.2 5.3 5.5 5.8 have many different detail.

my be use can try

QStyleOption opt;  
opt.initFrom(this);  
QPainter p(this);  
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);