Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could Qt apply style from an external Qt Stylesheet file?

I would like the users to be able to customize the default look of our applications by simply loading their OWN Qt Style-sheet files. How do we accomplish that? Can anybody give me a head start?

like image 717
Owen Avatar asked Dec 15 '10 08:12

Owen


People also ask

Can external style sheet be cached?

External CSS is cached (assuming you are using right http expire headers). However, it uses some of the cache space of the browser. It's not a very big concern, but is something worth keeping in mind with mobile devices becoming more prominent.

Does Qt Designer use CSS?

Qt Style Sheet terminology and syntactic rules are almost identical to those of HTML CSS.


2 Answers

Say the user have its stylesheet named stylesheet.qss and is located in the application folder.

You could load the style sheet when starting the application, using the -stylesheet argument :

myapp->stylesheet = stylesheet.qss; 

But this require your user to know how to start an application with arguments.

What you could also do is to add a settings dialog in your app, where the user can choose a stylesheet path.

You can then open this file, load the content, and set it to your application with QApplication::setStyleSheet() :

 QFile file("stylesheet.qss");  file.open(QFile::ReadOnly);  QString styleSheet = QLatin1String(file.readAll());   qApp->setStyleSheet(styleSheet); 

Qt is providing an example online which might be helpful.

like image 70
Jérôme Avatar answered Sep 19 '22 19:09

Jérôme


You just set the style sheet for the entire application based on configuration provided by the customer.

http://doc.qt.io/qt-5/qapplication.html#styleSheet-prop

You could set/get this configuration from any number of places, a properties dialog in the application is probably the most natural approach.

like image 39
James Gaunt Avatar answered Sep 18 '22 19:09

James Gaunt