[PyQt] QPixmap.loadFromData()

paolo at paolodestefani.it paolo at paolodestefani.it
Mon Jan 28 10:20:59 GMT 2013


I changed my code in this way, converting bytea field to a text field:

def createEditor(self, parent, option, index):
         dd = DisplayDialog(parent)
         ba = index.model().data(index, Qt.DisplayRole)
         if ba:
             pix=QPixmap()
             print("Load", pix.loadFromData(QByteArray.fromBase64(ba), 
"PNG"))
             dd.lbimage.setPixmap(pix)
         if dd.exec_() == QDialog.Accepted:
             pix=dd.getImage()
             ba = QByteArray()
             buf = QBuffer(ba)
             buf.open(QIODevice.WriteOnly)
             print("Save", pix.save(buf, "PNG"))
             index.model().setData(index, ba.toBase64(), Qt.EditRole)
             index.model().submit()

In this way everything is working fine. Of course i've also change the 
image field of the table from bytea to text
But i still don't understand why the 'old' code isn't working.
Maybe the bytea field type is supported by Qt but not in the sql model 
classes ?



Il 2013-01-25 00:41 paolo at paolodestefani.it ha scritto:
> Hello
> I'm trying to create a item delegate for a sql table (postgres db)
> where i have a field (type bytea) that should contain a PNG image.
> This is the code of the create editor method:
>
> def createEditor(self, parent, option, index):
>         dd = DisplayDialog(parent)
>         ba = index.model().data(index, Qt.DisplayRole)
>         if ba:
>             pix=QPixmap()
>             print("Load", pix.loadFromData(ba, "PNG"))
>             dd.lbimage.setPixmap(pix)
>         if dd.exec_() == QDialog.Accepted:
>             pix=dd.getImage()
>             ba = QByteArray()
>             buf = QBuffer(ba)
>             buf.open(QIODevice.WriteOnly)
>             print("Save", pix.save(buf, "PNG"))
>             index.model().setData(index, ba, Qt.EditRole)
>             index.model().submit()
>
> I want that when te user click in the cell that will contain the PNG
> image and a dialog popup where a can choose an PNG image file. This
> dialog have a label that i used for displaying the image (dd.lbimage)
> But when i test this i see:
>
> Load False
> Save True
>
> which means (i guess) tha i can store the image in the db converting
> it from PNG to QByteArray but when i retrieve it a can not convert
> from QByteArray to PNG image.
>
> What's wrong in my code ?



More information about the PyQt mailing list