1

I am developing a cross-platform application, and it looks way worse in Windows that in Ubuntu. This has to do only with the default style sheet for Windows vs the one for Ubuntu. Is there a way to obtain the OS-dependent style sheet that is applied to an application? I have tried to retrieve the application style sheet with:

QApplication app(argc, argv);
qDebug() << "Style sheet: " << app.styleSheet();

But this produces an empty string (unless you have manually set it to something). My intention is to retrieve the Ubuntu style sheet and save it to a file (and add it to the resources list), so I can then load it for the application in both OS:

QApplication app(argc, argv);
QFile file(":/ubuntu_style.qss");
file.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(file.readAll());
app.setStyleSheet(styleSheet);

Any ideas on how to get the default style sheet of the OS?

apalomer
  • 1,895
  • 14
  • 36

2 Answers2

2

I found the answer in another forum. I post it here for future search of other users.

You can get OS default styles using QStyleFactory. A list of possible style is available using QStyleFactory::key() returns a list of available keys. In my case, this always returned "Windows" and "Fusion" both in Windows and Linux OSs. Moreover, in Windows, I also have the "windowsvista". Using this class, I can set the app main style with:

QApplication app(argc, argv);
app.setStyle(QStyleFactory::create("Fusion"));

So my app always looks the same regardless of the OS in which it is compiled/used.

However, this does not provide a style sheet, it provides a QStyle, so using app.styleSheet(); returns an empty string. Reading through another Stack Overflow question, and especially the comments to its accepted answer it is possible to better understand QStyle and qt style sheets. And conclude that, it is not possible to convert a QStyle into a style sheet as these are things working on different layers. (special thanks to the comment posted in there by @lcikgl)

apalomer
  • 1,895
  • 14
  • 36
0

According to documentation, stylesheet() will return an empty string (which is your case) when you don't specify a valid path using the argument -stylesheet. I think that you don't do so. So, you need to add this argument for your application. Alternatively, make your own qss file and add it to the resources folder. I hope this helps you solve your problem.

Mohammed Deifallah
  • 1,290
  • 1
  • 10
  • 25