Skip to content

Instantly share code, notes, and snippets.

@seanwu1105
Created June 29, 2018 12:18
Show Gist options
  • Save seanwu1105/4e9362cadedec5353874ef8e28463c2d to your computer and use it in GitHub Desktop.
Save seanwu1105/4e9362cadedec5353874ef8e28463c2d to your computer and use it in GitHub Desktop.
Using pyrcc5 to Help Pyinstaller Import External Sources Example

Using pyrcc5 to Help Pyinstaller Import External Sources Example

Dependencies

PyQt5

pip install PyQt5

Example Usage

pyrcc5 src.qrc -o external_src.py
python3 qfile_example.py
This is the contents of external_src file.
# -*- coding: utf-8 -*-
# Resource object code
#
# Created by: The Resource Compiler for PyQt5 (Qt v5.10.1)
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore
qt_resource_data = b"\
\x00\x00\x00\x2a\
\x54\
\x68\x69\x73\x20\x69\x73\x20\x74\x68\x65\x20\x63\x6f\x6e\x74\x65\
\x6e\x74\x73\x20\x6f\x66\x20\x65\x78\x74\x65\x72\x6e\x61\x6c\x5f\
\x73\x72\x63\x20\x66\x69\x6c\x65\x2e\
"
qt_resource_name = b"\
\x00\x0c\
\x0a\x21\x33\xa3\
\x00\x65\
\x00\x78\x00\x74\x00\x65\x00\x72\x00\x6e\x00\x61\x00\x6c\x00\x5f\x00\x73\x00\x72\x00\x63\
"
qt_resource_struct_v1 = b"\
\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\
"
qt_resource_struct_v2 = b"\
\x00\x00\x00\x00\x00\x02\x00\x00\x00\x01\x00\x00\x00\x01\
\x00\x00\x00\x00\x00\x00\x00\x00\
\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\
\x00\x00\x01\x64\x4b\x72\x26\xa2\
"
qt_version = QtCore.qVersion().split('.')
if qt_version < ['5', '8', '0']:
rcc_version = 1
qt_resource_struct = qt_resource_struct_v1
else:
rcc_version = 2
qt_resource_struct = qt_resource_struct_v2
def qInitResources():
QtCore.qRegisterResourceData(rcc_version, qt_resource_struct, qt_resource_name, qt_resource_data)
def qCleanupResources():
QtCore.qUnregisterResourceData(rcc_version, qt_resource_struct, qt_resource_name, qt_resource_data)
qInitResources()
from PyQt5.QtCore import QFile, QIODevice
import external_src
def main():
# Use QFile to load external sources compliled by pyrcc5.
f = QFile(':/external_src')
if not f.open(QIODevice.ReadOnly):
raise IOError('file open error')
print(str(f.readAll(), 'utf-8'))
f.close()
if __name__ == '__main__':
main()
<!DOCTYPE RCC><RCC version="1.0">
<qresource>
<file>external_src</file>
</qresource>
</RCC>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment