Hamid Ohadi
Hamid Ohadi

Reputation: 65

Python crashes using pyqt, setPixmap and QLabel

I'm using this python code on a windows 7 64-bit machine, which simply displays a randomly generated black and white image. If I make the image larger than 511x511 pixels, my python 2.7 console crashes. It works fine on my Mac though. Any ideas?

import sys
from PyQt4 import QtGui, QtCore
import numpy as np


class PixmapTest(QtGui.QWidget):

    def __init__(self):
        super(PixmapTest, self).__init__()

        imglayout = QtGui.QHBoxLayout(self)

        size = 512
        img_8bit = (256*np.random.random((size,size))).astype(np.uint8)     

        img = QtGui.QImage(img_8bit.repeat(4), size, size, QtGui.QImage.Format_RGB32)

        pixmap = QtGui.QPixmap(img)

        imglabel = QtGui.QLabel(self)
        imglabel.setPixmap(pixmap)

        imglayout.addWidget(imglabel)
        self.setLayout(imglayout)

        self.show()        

def main():

    app = QtGui.QApplication(sys.argv)
    form = PixmapTest()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Upvotes: 1

Views: 1433

Answers (1)

101
101

Reputation: 8989

Strange, for me (Win7, 64-bit) the cut off size is closer to 300. I don't know why it behaves like this but in my case I can fix it by calling processEvents on the application, like this:

class PixmapTest(QtGui.QWidget):
    def __init__(self, app):
        super(PixmapTest, self).__init__()
        self.app = app
        imglayout = QtGui.QHBoxLayout(self)
        size = 333
        img_8bit = (256*np.random.random((size,size))).astype(np.uint8)     
        img = QtGui.QImage(img_8bit.repeat(4), size, size, QtGui.QImage.Format_RGB32)
        pixmap = QtGui.QPixmap(img)
        imglabel = QtGui.QLabel(self)
        imglabel.setPixmap(pixmap)
        imglayout.addWidget(imglabel)
        self.setLayout(imglayout)
        self.show()        
        self.app.processEvents()

def main():
    app = QtGui.QApplication(sys.argv)
    form = PixmapTest(app)
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Upvotes: 5

Related Questions