c++qtqapplicationqstyle

How does Qt select a default style?


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?


Solution

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

    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!");