I’m working on a small app and I’m having difficulty importing a QML file from a QRC resource file.
My QRC is as follows:
<RCC>
<qresource prefix="/qml">
<file alias="ProductionPane">qml/view/ProductionPane.qml</file>
<file alias="SideBar">qml/view/SideBar.qml</file>
<file alias="TestingView">qml/view/TestingView.qml</file>
<file alias="LogView">qml/view/LogView.qml</file>
<file alias="StatsView">qml/view/StatsView.qml</file>
<file alias="MyButton">qml/comp/MyButton.qml</file>
</qresource>
<qresource prefix="/assets">
<file alias="Logo">assets/img/logo.png</file>
</qresource>
<qresource prefix="/">
<file>main.qml</file>
</qresource>
</RCC>
The files are “physically” organized in the following manner:
main.qml
main.py
rc_resources.py
qml/view/LogView.qml
qml/view/ProductionPane.qml
qml/view/SideBar.qml
qml/view/StatsView.qml
qml/view/TestingView.qml
qml/comp/MyButton.qml
assets/img/logo.png
Here’s my main.py file:
import sys
from pathlib import Path
from PySide6.QtGui import QGuiApplication
from PySide6.QtQml import QQmlApplicationEngine
import rc_resources
if __name__ == "__main__":
app = QGuiApplication(sys.argv)
engine = QQmlApplicationEngine()
engine.addImportPath("qrc:/view")
engine.load(QUrl("qrc:/main.qml"))
if not engine.rootObjects():
sys.exit(-1)
sys.exit(app.exec())
And finally, my main.qml file:
import QtQuick
import QtQuick.Window
import QtQuick.Controls
import "qrc:/qml/ProductionPane"
import "qml/view"
Window {
id: mainWindow
minimumWidth: 1024
minimumHeight: 768
width: 1920
height: 1080
title: qsTr("FooBar")
ProductionPane {
id: productionPane
anchors.fill: parent
}
SideBar {
onViewChanged: productionPane.loaderSource = view
}
}
Without import "qml/view"
in main.qml
, the ProductionPane
and SideBar
objects aren’t recognized in QT creator.
I verified (in main.py
after instantiation of the QQmlApplicationEngine
object) that QFileInfo(":/qml/ProductionPane").exists()
returns true, so I know that the engine can find the QML file in the QRC file, but importing it isn’t working, as I’m getting the following error when running it:
qrc:/main.qml:4:1: "qrc:/qml/ProductionPane": no such directory
I get this error when I use "qrc:/qml/ProductionPane"
.
":/qml/ProductionPane"
results in an IDE error File or directory not found.
Does anyone have any suggestions?
Thanks!