Search code examples
pythonqmlpyside6

issue using Pyside and qml when trying to load qml


I'm trying to do the basic step when creating a Qt project using python. I'm already familiar with Qt with C++ and the syntax are basicaly the same. But I'm can't import my main.qml file using qrc file (or absolute path btw). The main file just finished with exit code -1 because root object of engine object is not set / empty.

My project implementation is:

├── main.py
└── qml
    ├── main.qml
    └── qml.qrc
    └── qml_rc.py

qml.qrc :

<!DOCTYPE RCC><RCC version="1.0">
    <qresource prefix="/">
        <file alias="main.qml">main.qml</file>
    </qresource>
</RCC>

main.py

import os

from PySide6 import QtGui, QtCore, QtQml
from PySide6.QtCore import QUrl
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
import qml.qml_rc
import sys

if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    engine.load(QUrl("qrc:/main.qml")) ## I already use the full path, or the alias 
                                       ##  declared in qrc file
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())

main.qml

import QtQuick 2.15
import QtQuick.Controls 2.15

ApplicationWindow {
    visible: true
    width: 600
    height: 500
    title: "HelloApp"

    Text {
        anchors.centerIn: parent
        text: "Hello World"
        font.pixelSize: 24
    }

}

I also use the command line: pyrcc5 qml.qrc -o qml_rc.py to convert qrc file into python file and importing it into the main.py but nothing work.

I miss something in this project but I can't understand the reason. What I Do wrong ?


Solution

  • If you are using PySide6 then use the tools provided by PySide6, not PyQt5, so run:

    pyside6-rcc qml.qrc -o qml_rc.py