How to deploy QML with PyInstaller?
-
Hello,
I have a small python QML app that I'm trying to deploy to an exe file. I found this SO answer to this issue.
I wanted to use option #3. Specifically add my
qml
files to the QT Resource system using a QRC file and then compile it and import it into python.The answer states that if your folder stracture is
project │ main.py │ main.qml
You should create a qrc file like so:
qml.qrc:
<RCC> <qresource> <file>main.qml</file> </qresource> </RCC>
Then convert the qml to py like this:
pyside2-rcc qml.qrc -o qml_rc.py
And then you're good to go.
The issue I'm having is, that I have multiple QML components, like so:
project │ QML | |-main.qml | |-MainComponent.qml | |-RowComponent.qml │ main.py
I've made a qrc file like the answer suggested:
<RCC> <qresource> <file>QML/main.qml</file> <file>QML/MainComponent.qml</file> <file>QML/RowComponent.qml</file> </qresource> </RCC>
And then I followed the rest of the instructions.
When I give the uri of
":/QML/main.qml"
to the engine like so:engine = QQmlApplicationEngine() engine.load(":/QML/main.qml")
It says it fails to load a component named MainComponent
Warning: file::/QML/main.qml:51:5: MainComponent is not a type (file::/QML/main.qml:51, file::/QML/main.qml)
Because all my QML files are in the same folder, I reference them just by calling the type name. However this seems to be insufficient when using this method.
I think I'm missing an import statement in my
main.qml
that would tell the engine where to file MainComponent. But I don't know what import statement it should be.Could anyone help me out?
-
OK, after more searching I found this answer form the same helpful author.
He says that when the qml files aren't in the root folder you should add
import "qrc:/QML
in the main file so it could see other components in the qrc file. (This path is specfic to my case because my qml files are in./qml
folder.)On thing to note that might seem obvious but I spent quite a few tries to figure it out is that you must compile the qrc file
to py after every change you make to the qml files.Meaning running
pyside2-rcc qml.qrc -o qml_rc.py
. Otherwise the imports won't update, and you'll be stuck trying to figure out why the changes you're making don't affect anything.