Qt Widgets global stylesheet
-
Does Qt allow to set stylesheets application-wide? It's very tedious to set them to each control by hand. It would be perfect if Qt allows to work with stylesheets like with an ordinary CSS - to write global selectors, make strict targeting with "id" attribute and so on.
-
Yes !
And its also recommended to do so
as setting on each widget becomes a mess.
QApplication have method.
http://doc.qt.io/qt-5/qapplication.html#styleSheet-propand yes, it has selectors as ClassType, name etc
-
@Tikani said in Qt Widgets global stylesheet:
@mrjj
Ok, I understood that.
Then I want to put a huge string literal that contains global stylesheet to project resources and pass it to the method you pointed above. Does Qt allow to move "hardcoded" strings to resources?I think that the best practise for qt stylesheet is to use a ressource file on the qt ressource file named
stylesheet.qss
for example,You stylesheet.qss will contain this for example:
stylesheet.qss
QWidget
{
background-color: red;
}
QPushButton
{
color: yellow;
}
and then on your application you have to do something like this:QFile File("stylesheet.qss"); File.open(QFile::ReadOnly); QString StyleSheet = QLatin1String(File.readAll()); qApp->setStyleSheet(StyleSheet);
I hope this can help you !
-
Hi
And for fooling around and testing you can do also doauto teststyle = R"( QWidget { background-color: red; } QPushButton { color: yellow; } )"; int main(int argc, char* argv[]) { QApplication a(argc, argv); a.setStyleSheet(teststyle); MainWindow w; w.show(); return a.exec(); } (c++ 11 compiler)
-
If you want stylesheet ready for the use of qss in your application use this site:
https://qss-stock.devsecstudio.com