[PyQt] PyQt4 Python File Object to QFile

Russell Valentine russ at coldstonelabs.org
Tue Feb 16 07:27:33 GMT 2010


Russell Valentine wrote:
> I wonder if anyone ever bridged the gap between QFile and the Python 
> File Object? It could be handy sometimes to go between the two.
> 
..
> 
> How about for example if I wanted to open a file from a zip file.
> 
> zfile = zipfile.ZipFile("test.zip")
> f=zfile.open("some.png", 'r')
> qtf = QPythonFile(f)
> img=QImage()
> img.load(qtf)
> pixmap=QPixmap.fromImage(img)
> 
> f is a python file object but, how can I use this in Qt? I realize I 
> could save it to some temp file and reopen it with QFile but that seems 
> unelegant.
> 

This works for me for now. though I'm not having to read huge files 
currently.

zfile=zipfile.ZipFile("test.zip")
p=zfile.read("some.png")
img=QImage()
img.loadFromData(p, "PNG")
pixmap=QPixmap.fromImage(img)


Another way would be to load up data in a QByteArray and put it in 
QBuffer, but that still loads everything in memory, if you wanted to 
just stream some big file in a zip.

Just thought I'd followup.


Russell Valentine


More information about the PyQt mailing list