Jacob Frey
Jacob Frey

Reputation: 3

Trouble with setting QT style to external QSS file

I found a QSS file online (http://tech-artists.org/forum/showthread.php?2359-Release-Qt-dark-orange-stylesheet) that I would like to use as the style in my app. I have been trying multiple different ways of importing this file into my program, but every time I run my program, it does not successfully open the file.
Currently I have the file saved as myStyle.qss in the same directory as my project. I have also been hearing about inserting the file into a qrc file. Is this necessary in order to open it, or just a convenient way of storing the file? My code so far is:

    QApplication a(argc, argv);
    QFile file(":/myStyle.qss");
    file.open(QIODevice::ReadOnly);
    QString style(file.readAll());
    a.setStyleSheet(style);
    file.close();
I have seen this block of code in multiple different places, so I am fairly certain that I have the majority of it right and that my main problem is just in my file placement, or that I have the wrong file path written down. Thanks!

Upvotes: 0

Views: 1131

Answers (1)

Jablonski
Jablonski

Reputation: 18524

":/myStyle.qss"it is path which uses for Qt resource system. As you said, this file in the same directory, so try to set nornal path, "myStyle.qss", but be carefully because Qt will search this file in the build directory, so you should place file in build directory.

But when your qss file will be done, then save it in the resources. There are many examples how to do this in the web (for example this. In this case you will need your current path ":/myStyle.qss"

Why do you need resource?

Resources are build into exe file so you never lost this files, user can't delete it or rewrite. If user delete your qss file, all your style will fail, so you should protect it.

Upvotes: 1

Related Questions