Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[RESOLVED] StyleSheet file... it's possible?



  • Hi guys, I'm again...
    Well, I'm trying to decorate my application with CSS.
    I want to set a style for all buttons, for every element of my application, but I was thinking to use a file "mystyle.css" for example to put all CSS codes inside read this file and set this styles on my application... This is possible?

    I'm reading the docs and at least I don't found nothing about this... I will keep reading...

    Thanks all...



  • Yes. Load the file (perhaps from a resource) at application startup into a QString, and then call setStyleSheet on your QApplication instance.



  • Thanks Andre!



  • @
    QFile fileDark(":/qss/vc2.qss");
    fileDark.open(QFile::ReadOnly);
    app.setStyleSheet(fileDark.readAll());
    @

    qss is a css file, it is embedded as qresource



  • [quote author="broadpeak" date="1347370429"]@
    ...[/quote]
    You're spoiling all the "this is left as an excersise for the reader" fun! ;-)



  • Thanks broadpeak.
    I'm already using here, with .css and qresource. Works very well.

    Thanks.



  • [quote author="Andre" date="1347370510"][quote author="broadpeak" date="1347370429"]@
    ...[/quote]
    You're spoiling all the "this is left as an excersise for the reader" fun! ;-)

    [/quote]

    Ahhhhhhhhhh! Sorry :) I will never.... :)



  • By the way: note that your (broadpeak) method has a potential danger. You implicitly cast a QByteArray to a QString: QFile::readAll() returns a QByteArray, while setStyleSheet takes a QString. That means that you assume that the .css file will be ASCII (see the docs for QString). That might or might not be a valid assumption... I prefer to use a [[doc:QTextStream]] to read a text file instead.



  • I use to read some files like the "broadpeak" code!
    Nothing wrong at all, but I will read about the QTextStream!

    Thanks Andre.



  • [quote author="Andre" date="1347371510"]...that you assume that the .css file will be ASCII ... That might or might not be a valid assumption... I prefer to use a [[doc:QTextStream]] to read a text file instead.
    [/quote]

    Yes, of course, you are right (as usual :))!
    But I always use simple ASCII editor for css editing, because I never have special character in my qss file.



  • Well, for instance, the file names for images or object names for widgets may contain unicode characters, especially for developers from non-English speaking countries...



  • Well, I'm from Brazil and here we use the special chars (çãáâü....) but in source codes and etc... we avoid them... or I void this chars.

    But for safe reasons appears to me better use the "QTextStream".


Log in to reply