我有一个简单的PyQt应用程序。您可以使用pageup和pagedown键在图像文件夹中循环。
我正在使用QLabel的像素图来显示图像。在您评论processEvents调用之前,以下逻辑实际上有效。
关于崩溃原因的任何想法以及添加processEvents的原因可以防止崩溃?
# Python version: 2.7.9
# Qt version: 4.8.6
# PyQt version: 4.11.3
import sys
import os
from PIL import Image
from PIL import ImageOps
from PIL.ImageQt import ImageQt
from PyQt4 import QtGui
from PyQt4 import QtCore
from PyQt4 import Qt
imagefolder = "C:/Users/alan/Desktop/imagefolder"
class QExampleLabel (QtGui.QLabel):
def __init__(self, parentQWidget = None):
super(QExampleLabel, self).__init__(parentQWidget)
self.initUI()
def initUI (self):
self.imagelist = []
for path, subdirs, files in os.walk(imagefolder):
path = path.replace("\\", "/")
for file in files:
base, ext = os.path.splitext(file)
if ext.lower() == ".jpg":
jpg = path + "/" + file
self.imagelist.append(jpg)
self.i = -1
def keyPressEvent(self, keyevent):
event = keyevent.key()
if event == QtCore.Qt.Key_PageUp:
self.backward()
if event == QtCore.Qt.Key_PageDown:
self.forward()
def forward(self):
self.i = self.i + 1
if self.i >= len(self.imagelist):
self.i = len(self.imagelist) - 1
self.displayimage()
def backward(self):
self.i = self.i - 1
if self.i < 0:
self.i = 0
self.displayimage()
def displayimage(self):
photo = self.imagelist[self.i]
img = Image.open(photo)
mysize = (260,260)
method = Image.NEAREST if img.size == mysize else Image.ANTIALIAS
self.cropimage = ImageOps.fit(img, mysize, method = method, centering = (0.5,0.5))
qim = ImageQt(self.cropimage)
pix = QtGui.QPixmap.fromImage(qim)
self.setPixmap(pix)
QtGui.QApplication.processEvents() # comment this line to see setPixmap crash
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myQExampleLabel = QExampleLabel()
myQExampleLabel.show()
myQExampleLabel.resize(260,260)
sys.exit(app.exec_())
答案 0 :(得分:5)
错误可能是由垃圾收集引起的,因为您没有保留对Qt图像对象的显式引用。当您从QImage创建像素图时,基础数据将保持共享,因此使用QImage复制构造函数获取显式引用,然后保持活动状态,直到不再需要像素图:
self._img = QtGui.QImage(qim)
pix = QtGui.QPixmap.fromImage(self._img)
<强>更新强>:
使用测试用例,我只需保留对原始ImageQt
对象的引用即可消除崩溃:
self.qim = ImageQt(self.cropimage)
pix = QtGui.QPixmap.fromImage(self.qim)
self.setPixmap(pix)
或者更简单地说,使用QPixmap.detach():
qim = ImageQt(self.cropimage)
pix = QtGui.QPixmap.fromImage(qim)
pix.detach()
self.setPixmap(pix)