Search code examples
pythonqmlstylespyside2qapplication

PySide2 + QML : QApplication: invalid style override passed, ignoring it


I'm building a simple app using QML + PySide / Python as backend.

I'm trying to use "Universal" qml style. I'm subclassing QApplication and adding -style Universal argument :

class MyApp(QApplication):

def __init__(self, args):

    qt_args = [args[0], '-style', 'Universal'] + args[1:]
    super(MyApp, self).__init__(qt_args)

It works (my app uses Universal style) but it throws the following warning:

QApplication: invalid style override passed, ignoring it.
Available styles: windowsvista, Windows, Fusion

It seems that PySide2 has troubles getting the standard QML styles (Universal / Material)

Does anyone know how to deal with it?

Thanks.


Solution

  • Explanation:

    The "-style" argument is used for 2 purposes:

    • Set the style of the Qt Widgets
    • Set the style of Qt Quick Controls.

    So since QApplication is designed to handle the Qt Widgets, try first to verify if the style exists for the Qt Widgets but in your case it fails by issuing the warning you get, and then try to establish the style of Qt Quick Controls.

    Solution:

    So the solution is to find another alternative to not have that confusion:

    1. Environment variable: Set the style through the QT_QUICK_CONTROLS_STYLE environment variable:

      import os
      os.environ["QT_QUICK_CONTROLS_STYLE"] = "Universal"
      
    2. Configuration file:

      2.1 Create the file "qtquickcontrols2.conf" with the following content

      ; This file can be edited to change the style of the application
      ; See Styling Qt Quick Controls 2 in the documentation for details:
      ; http://doc.qt.io/qt-5/qtquickcontrols2-styles.html
      
      [Controls]
      Style=Material
      

      2.2 add it to a qresource,

      <RCC>
          <qresource prefix="/">
              <file>qtquickcontrols2.conf</file>
          </qresource>
      </RCC>
      

      2.3 compile the .qrc to .py using pyside2-rcc resource.qrc -o resource_rc.py or rcc --generator python resource.qrc -o resource_rc.py and

      2.4 import it into your application.

      import resource_rc