Search code examples
qtqtstylesheets

setStyleSheet from a resource QSS file


In my widget, I can do something like that:

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

This will set the widget background red.

I have a QSS file in my resources. How do I instruct my widget to take its style sheet content from there, vs just taking the QSS syntax as parameter?


Solution

  • Got it: you actually have to "read the file" from the resources, convert it to a QString and feed it to the setStyleSheet. E.g.:

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