How does Qt select a default style?
Asked Answered
S

2

5

In a Qt GUI application, QApplication::style()->objectName() will return the current style, for example "windowsvista".

How/where does it choose this default style, and what information does it use to decide?

Shuttering answered 4/1, 2018 at 10:17 Comment(0)
S
6

Qt comes with builtin styles, these are (on my 5.9.2):

  • Windows
  • WindowsXP
  • WindowsVista
  • Android
  • Fusion
  • Macintosh

each one having its own class, derived from QStyle.

To see which ones are available (it depends on Qt build configuration):

const auto & styles = QStyleFactory::keys();
for(const auto & s : styles)
{
    qDebug() << s;
}

Custom plugins (i.e. libraries in the QTDIR/plugins/styles directory) would be shown as well, if present.

How the default style is chosen?

Default style is searched in QApplication method style(), in qapplication.cpp file, in this order:

  1. The style override, if set by the environment variable QT_STYLE_OVERRIDE (this is set in QApplicationPrivate::process_cmdline());
  2. The style returned by QApplicationPrivate::desktopStyleKey() (this method loads a list of styles from the current platform theme and select the first name from this list that is present in the QStyleFactory::keys() list);
  3. The first item in QStyleFactory::keys() list.

If a style could not be determined, the function will assert

Q_ASSERT(!"No styles available!");
Steinberg answered 4/1, 2018 at 14:25 Comment(0)
M
2

In the documentation:

Qt contains a set of QStyle subclasses that emulate the styles of the different platforms supported by Qt (QWindowsStyle, QMacStyle etc.).

You can set the style by using a key: windowsvista for example, fusion, macintosh, etc. When using any key, the style returned will be a subclass of QStyle. Depending on the platform you're using, you will have access to a certain number of keys.

How/where does it choose this default style

It is done in the QStyleFactory source file. You can also take a look a the QStyle source file to get a hang of what's going on.

what information does it use to decide

The default style is platform dependent, and then you can choose any style among the keys at your disposition on this platform.

Milkandwater answered 4/1, 2018 at 14:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.