Python qt: resources
PySide can store images or other files in a python-compatible module for cross-platform portablility, and convenience (relative paths) while programming. The author used a qrc file (xml) to keep track of all files in a directory. See Rapid GUI Programming with Python and Qt (index 168, page 173).
QResource
Traditionally, I've abused python module's
__file__
attribute to locate peripheral datafiles (json, yaml, etc). If you are compiling your program, you lose the ability to use these. Qt allows you to embed these files into importable python modules using thepyrcc
(PyQt) orpyside-rcc
module.NOTE:
You'll need to use the pyrcc that matches your Qt version, and possibly the Qt bindings you are using.
NOTE:
pyside on windows hides it's pyside-rcc binary in site-packages/PySide/
in a hypothetical python package:
package/ media.qrc dbsetup.sql images/ fileA.png fileB.png
package/media.qrc<!DOCTYPE RCC><RCC version="1.0"> <qresource> <file>dbsetup.sql</file> <file>images/fileA.png</file> <file>images/fileB.png</file> </qresource> </RCC>
embed all files referenced inmedia.qrc
into the modulemedia.py
pyrcc -o media.py media.qrcAfter importing the file, you can reference it's contents by prefixing filepaths with
:
.from Qt import QtCore import media ## Read Embedded Files testfile = QtCore.QFile(':/dbsetup.sql') while not testfile.atEnd(): line = testfile.readLine() print( line ) ## Reference Embedded Files ## (in qss files, etc) QtWidgets.QImage( ':/images/fileA.png' )