Reputation: 213
Is it possible to have multiple stylesheets for a single qt application and select any of them as needed?
For instance, I would like to have different styles of push buttons within my application. I understand that normally you would have something like:
QPushButton { background-color: green; }
And then all your push buttons will have the green style as per the above line. However, I would like to have my stylesheet to look somehow like this:
QPushButton1 { background-color: blue; }
QPushButton2 { background-color: green; }
I may need to place several push buttons in my application and for each of those, I want to select either the QPushButton1 style or the QPushButton2 style.
Is this feasible to do within one or multiple stylesheets, so that I can use setStyleSheet() to enable my custom style?
Thanks!
Upvotes: 2
Views: 2570
Reputation: 18504
You can use global stylesheet and set special settings to each special object name.
#QPushButton1 { background-color: blue; }
#QPushButton2 { background-color: green; }
But in this case you also need to set these object names in code. It can be done with:
ui->pushbutton1->setObjectName("QPushButton1");
ui->pushbutton2->setObjectName("QPushButton2");
It is also possible to set same object name to different widgets.
About changing objectName
. When you change objectName
, Qt
will not re-apply styleSheet
automatically (it was done for better performance), so you should do this manually. You can use objectNameChanged
signal and re-apply styleSheet
in corresponding slot or just something like:
ui->pushButton_2->setObjectName("QPushButton1");
qApp->setStyleSheet(qApp->styleSheet());//re-apply
Upvotes: 3