Why this code show me a window without image? I try to use QByteArray instead of StringIO, but i have same result. Anyone know answer?
import sys
from PyQt4 import QtGui, QtCore
from StringIO import StringIO
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
cs=StringIO()
cs.write(open("image.jpeg",'rb').read())
p = QtGui.QPixmap()
p.loadFromData(cs.getvalue())
lbl=QtGui.QLabel()
lbl.setPixmap(p)
layout = QtGui.QHBoxLayout(self)
layout.addWidget(lbl)
self.move(20, 30)
self.setWindowTitle('Picture')
self.show()
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
UPDATE:
I need show image from StringIO (or any another object in RAM), because i downloading it with pycurl without saving to HDD.
UPDATE 2:
Added Layout using, like Petr Viktorin advised, but code still not working.
UPDATE 3:
@Petr Viktorin, this code work only when i want to display PNG file, but when image – Jpeg/jpg, i see empty form. Maybe problem in QPixmap?
UPDATE 4:
Tested with various images, but…
http://i39.tinypic.com/3535mqd.jpg Where problem?
You didn’t add the label to the mainwindow. The easiest solution is:
And the proper solution is using some layout, so the window will have the appropriate size. Add this after
lbl.setPixmap(p):