Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QT: setStyleSheet from a resource QSS file?

Tags:

qt

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?

like image 807
JasonGenX Avatar asked Jan 26 '11 22:01

JasonGenX


1 Answers

As an alternative to setting a style sheet for each widget, you can just load and set a stylesheet for a whole application. Something like this:

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 pick their styles from the given stylesheet automatically.

like image 70
Roman Kruglov Avatar answered Sep 24 '22 09:09

Roman Kruglov