QT: setStyleSheet from a QSS file resource?

In my widget, I can do something like this:

MyWindow::MyWindow(QWidget *parent) : QWidget(parent) { ui.setupUi(this); setStyleSheet("QWidget { background-color: red }"); // <--- HERE } 

This will set the widget's red background.

I have a QSS file in my resources. How can I instruct my widget to take the contents of a stylesheet instead of using qss syntax as a parameter?

+8
qt
source share
2 answers

Got this: you really need to โ€œread the fileโ€ from resources, convert it to QString and pass it to setStyleSheet. For example:.

 QFile file(":/qss/default.qss"); file.open(QFile::ReadOnly); QString styleSheet = QLatin1String(file.readAll()); setStyleSheet(styleSheet); 
+13
source share

As an alternative to setting a style sheet for each widget, you can simply download and set a style sheet for the entire application. Something like that:

 QApplication app( argc, argv ); // Load an application style QFile styleFile( ":/style.qss" ); styleFile.open( QFile::ReadOnly ); // Apply the loaded stylesheet QString style( styleFile.readAll() ); app.setStyleSheet( style ); 

In this case, all widgets will automatically select their styles from this stylesheet.

+13
source share

All Articles