PyInstaller and QML Files

点点圈 提交于 2021-02-10 16:50:14

问题


How can I include the QML file into my Python project as a single executable. When I run pyinstaller --onefile main.py, running the executable results in an error that the QML file is not found. Unless I use an absolute path or place view.qml in the same directory as my executable. I don't want to have a separate QML file, I want it combined into the executable.

main.py:

if __name__ == "__main__":
  app = QGuiApplication(sys.argv)
  engine = QQmlApplicationEngine()
  engine.load(QUrl("view.qml"))
  sys.exit(app.exec_())

view.qml:

import QtQuick 2.0

ApplicationWindow {
  id: window
  title: "Window"
  width: 900
  height: 600
  visible: true
}

回答1:


My answer in addition to showing how to use the possible duplicate answer in this particular case, also shows an alternative using Qt's own tools.

1. Copy the .qml to the same executable folder

In this case you have to build the absolute path of the qml using the application path.

import os
import sys

from PySide2 import QtCore, QtGui, QtQml

# https://stackoverflow.com/a/404750/6622587
application_path = (
    os.path.dirname(sys.executable)
    if getattr(sys, "frozen", False)
    else os.path.dirname(os.path.abspath(__file__))
)


if __name__ == "__main__":
    import os
    import sys

    app = QtGui.QGuiApplication(sys.argv)
    engine = QtQml.QQmlApplicationEngine()
    file = os.path.join(application_path, "main.qml")
    engine.load(QtCore.QUrl.fromLocalFile(file))
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())

Then copy the .qml to the same executable folder.

2. Add .qml to as data files

The data files are decompressed in the folder relative to sys._MEIPASS, if the --onefile option is not used then that path is the executable folder otherwise it will be decompressed in the temporary folder.

In your case it implements the following:

├── main.py
└── main.qml

main.py

import os
import sys

from PySide2 import QtCore, QtGui, QtQml

# https://stackoverflow.com/a/42615559/6622587
application_path = (
    sys._MEIPASS
    if getattr(sys, "frozen", False)
    else os.path.dirname(os.path.abspath(__file__))
)


if __name__ == "__main__":
    import os
    import sys

    app = QtGui.QGuiApplication(sys.argv)
    engine = QtQml.QQmlApplicationEngine()
    file = os.path.join(application_path, "main.qml")
    engine.load(QtCore.QUrl.fromLocalFile(file))
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())

And run pyinstaller as follows:

pyinstaller --add-data "main.qml:." --onefile main.py

3. Use Qt Resource

You can create a .qrc that adds the qml, then convert them to .py and finally include it in the .py.

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

main.py

import sys

from PySide2 import QtCore, QtGui, QtQml

import qml_rc


if __name__ == "__main__":
    import os
    import sys

    app = QtGui.QGuiApplication(sys.argv)
    engine = QtQml.QQmlApplicationEngine()
    engine.load(":/main.qml")
    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())

qml.qrc

<RCC>
  <qresource prefix="/">
    <file>main.qml</file>
  </qresource>
</RCC>

To convert the qml.qrc to .py you must use the following command:

pyside2-rcc qml.qrc -o qml_rc.py 

and finally as it is already a .py we only run pyinstaller:

pyinstaller main.py --onefile


来源:https://stackoverflow.com/questions/58035550/pyinstaller-and-qml-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!