[PySide] How to make a QIcon from .png byte string (can't use QByteArray due to #717)

877 views
Skip to first unread message

Eli Stevens (Gmail)

unread,
Apr 14, 2011, 5:35:41 PM4/14/11
to pys...@lists.pyside.org
I have a .png file that I need to access via
pkg_resources.resource_string (or _stream). I want to turn this file
into a QIcon, and so I tried the following:

data_str = pkg_resources.resource_string('my.package',
os.path.join('img', 'my.png'))

qba = QByteArray.fromRawData(buffer(data_str))
print >>sys.stderr, len(data_str), qba.length(),
data_str.index('\x00'), repr(qba)

assert len(data_str) == qba.length() # This fails due to the
qba having stopped at the first null byte in data_str.

qimg = QImage.fromData(qba, 'PNG')
qpix = QPixmap.fromImage(qimg)
qicon = QIcon(qpix)

However, due to bug 717 (I suggest bumping the priority on that bug -
it seems pretty serious to not be able to have nulls in QByteArrays),
it doesn't work. Likewise, QImage.fromData(data_str, 'PNG') doesn't
seem to work either.

http://bugs.pyside.org/show_bug.cgi?id=717

Does anyone have any suggestions on how to connect the dots?

Thanks,
Eli
_______________________________________________
PySide mailing list
PyS...@lists.pyside.org
http://lists.pyside.org/listinfo/pyside

Eli Stevens (Gmail)

unread,
Apr 14, 2011, 6:48:22 PM4/14/11
to pys...@lists.pyside.org
I found a workaround for my purposes:

qba = QByteArray()
qba.append(data_str, len(data_str))

I'll update the bug with as much.

JesterEE

unread,
Aug 20, 2011, 1:11:50 AM8/20/11
to pys...@googlegroups.com, pys...@lists.pyside.org
Eli,

Update to your code snippet with some stuff I was experimenting with in the systray.py example.  I am using PySide 1.0.5 on Python 2.7 Windows x64:

        data_str = open('C:\\Path\\To\\Image\\image.png', 'rb').read()
        qimg = QtGui.QImage.fromData(data_str, 'PNG')
        qpix = QtGui.QPixmap.fromImage(qimg)
        qicon = QtGui.QIcon(qpix)
        self.iconComboBox.addItem(QtGui.QIcon(qicon), "Image")

I did not use the release you were developing with as I am brand new to PySide, but from my experimentation, you don't need the QByteArray at all.

Hope this helps someone else int he future.

-JesterEE
Reply all
Reply to author
Forward
0 new messages