[PySide] Help with resource files

Sean Fisk sean at seanfisk.com
Mon Apr 8 23:04:48 CEST 2013


OK, I have an interim solution. While I do believe that QFile.readAll() should return the file contents as a QByteArray, QResource.data() seems to work! Here is the code: 
#!/usr/bin/env python # test.py import PySide from PySide import QtCore import resources # Versions print 'Running PySide {0} / Qt {1}'.format( PySide.__version__, QtCore.__version__) print 'PySide compiled with Qt {0}'.format(QtCore.qVersion()) # Read resource test_txt = QtCore.QResource(':/test.txt') data = test_txt.data() print "type(data()): {0}".format(type(data)) print "data(): `{0}'".format(data)
which yields
Running PySide 1.1.2 / Qt 4.8.4 PySide compiled with Qt 4.8.4 type(data()): <type 'buffer'> data(): `This is just a test. '


This returns the resource as a Python buffer (http://docs.python.org/2/library/functions.html#buffer) type. I'm hoping I can coerce this into the source for a font or image in Qt.

More updates if I get it to work.

Sincerely,

-- 
Sean Fisk


On Monday, April 8, 2013 at 4:41 PM, Sean Fisk wrote:

> That's good to know, Eric. Eventually we will be doing Windows distribution so I'm glad there are no problems. 
> 
> Here is another update. I tried putting Lena on a button using QIcon on my OS X installation and it worked:
> 
> That leads me to believe it's a problem with QFile, not the actual generated resource file. I'm going to try messing around with the QResource class next.
> 
> Thanks for all the help, guys!
> 
> Sincerely,
> 
> -- 
> Sean Fisk
> 
> 
> On Monday, April 8, 2013 at 3:18 PM, Eric Johnson wrote:
> 
> > Sean Fisk <sean <at> seanfisk.com (http://seanfisk.com)> writes:
> > 
> > > 
> > > Thanks for the confirmation with Python 2 and 3 on Ubuntu. Although we
> > > don't yet know whether it is a bug or just incorrect code by me.
> > > 
> > > 
> > 
> > 
> > Works fine for me on Windows (cygwin/bash shell but win32 version of
> > Python 2.7.2) if I open the file first. 
> > 
> > I'm surprised there is no exception thrown if you don't do this.
> > I can try on OS X tonight.
> > 
> > ...
> > # Read resource
> > test_txt = QtCore.QFile(':/test.txt')
> > test_txt.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Text)
> > print 'Does it exist?: {0}'.format(test_txt.exists())
> > print "readAll(): `{0}'".format(test_txt.readAll())
> > 
> > gives output
> > 
> > $ python test.py
> > Running PySide 1.1.0 / Qt 4.7.4
> > PySide compiled with Qt 4.7.4
> > Does it exist?: True
> > readAll(): `This is just a test.
> > '
> > 
> > Eric
> > 
> > 
> > 
> > _______________________________________________
> > PySide mailing list
> > PySide at qt-project.org (mailto:PySide at qt-project.org)
> > http://lists.qt-project.org/mailman/listinfo/pyside
> > 
> > 
> > 
> 
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20130408/2ec035fe/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: resource-qicon-lena.png
Type: image/png
Size: 15393 bytes
Desc: not available
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20130408/2ec035fe/attachment.png>


More information about the PySide mailing list