Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyinstaller: How to include QML file for a simple Pyside + QML Application?

I'm having trouble generating an exe using Pyinstaller. My biggest problem should be "including the qml file". I tried a lot, but still failed. Hope someone can show me how the spec file should be written in order to include the QML.

Generally, what I want, is to create a Windows Exe from my Pyside+QML Application. But how?

main.py

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from PySide.QtCore import *
from PySide.QtGui import *
from PySide.QtDeclarative import QDeclarativeView

# Create Qt application and the QDeclarative view
app = QApplication(sys.argv)
view = QDeclarativeView()
# Create an URL to the QML file
url = QUrl('view.qml')
# Set the QML file and show
view.setSource(url)
view.setResizeMode(QDeclarativeView.SizeRootObjectToView)
view.show()
# Enter Qt main loop
sys.exit(app.exec_())

view.qml

import QtQuick 1.0

Rectangle {
    width: 200
    height: 200
    color: "red"

    Text {
        text: "Hello World"
        anchors.centerIn: parent
    }
}
like image 564
Nicholas TJ Avatar asked Oct 19 '25 08:10

Nicholas TJ


1 Answers

Not sure about PySide, but PyInstaller as of 2.1 supports PyQt5. I'd assume that the general procedure is similar.

For PyQt5 put your qml files into a resource file, which you then compile using pyrcc5 (pyside-rcc). You then import the generated python module and PyInstaller will treat it like any other module.

It is also possible to include the qml files directly. By doing something like this:

extrafiles = [('myfile.qml', os.path.join('path', 'to', 'myfile.qml'), 'DATA')]

...

coll = COLLECT( exe,
               a.binaries + extralibs,
               a.zipfiles,
               a.datas + extrafiles,
               ...

You will probably also need to package the qml libraries from the qml dir returned by qmake -query QT_INSTALL_QML.

like image 117
glennr Avatar answered Oct 22 '25 05:10

glennr