How to use QtQuickControls2 Style in pyqt?

I had read a article The Imagine Style that can be used in qml with c++ code style.

Which that need qtquickcontrols2.conf and Imagine Style or maybe others files.

But I don’t know how to use it in pyqt style.

I want to know is it posssible to use The Imagine Style In pyqt ?

Answer

The question is similar to PyQt QML Material Design button background won’t change one so this post will be generic on how to use the styles of QtQuickControls 2, and my answer is based on translating the options from the official docs: Styling Qt Quick Controls 2 to python:

1. Using QQuickStyle in Python (Only available for PySide2 and PySide6)

from PySide2.QtQuickControls2 import QQuickStyle
# from PySide6.QtQuickControls2 import QQuickStyle

# ...
engine = QQmlApplicationEngine()
QQuickStyle.setStyle("Imagine")

2. Command line argument

app = QGuiApplication(sys.argv + ["-style", "Imagine"])

3. Environment variable

os.environ["QT_QUICK_CONTROLS_STYLE"] = "Imagine"

4. Configuration file

qtquickcontrols2.conf

[Controls]
Style=Imagine

resource.qrc

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

PyQt5

pyrcc5 resources.qrc -o resources_rc.py

PySide2:

rcc -g python resources.qrc -o resources_rc.py

or

pyside2-rcc resources.qrc -o resources_rc.py

And then you just have to import the module in your main file:

import resources_rc

Note: “Imagine” can be changed to any of the styles: Default, Fusion, Material and Universal.