没有循环代码和有效示例,很难确定您的问题。但是我有类似的测试应用程序,希望对您有所帮助。
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import Image
import ImageQt
import ImageEnhance
import time
class TestWidget(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.scene = QGraphicsScene()
self.view = QGraphicsView(self.scene)
self.button = QPushButton("Do test")
layout = QV@R_111_2419@Layout()
layout.addWidget(self.button)
layout.addWidget(self.view)
self.setLayout(layout)
self.button.clicked.connect(self.do_test)
def do_test(self):
img = Image.open('image.png')
enhancer = ImageEnhance.Brightness(img)
for i in range(1, 8):
img = enhancer.enhance(i)
self.display_image(img)
QCoreApplication.processEvents() # let Qt do his work
time.sleep(0.5)
def display_image(self, img):
self.scene.clear()
w, h = img.size
self.imgQ = ImageQt.ImageQt(img) # we need to hold reference to imgQ, or it will crash
pixMap = QPixmap.fromImage(self.imgQ)
self.scene.addPixmap(pixMap)
self.view.fitInView(QRectF(0, 0, w, h), Qt.KeepAspectRatio)
self.scene.update()
if __name__ == "__main__":
app = QApplication(sys.argv)
widget = TestWidget()
widget.resize(640, 480)
widget.show()
sys.exit(app.exec_())
要点:
如果您正在执行某些处理或sleep
循环,则需要调用QCoreApplication.processEvents()
以允许Qt进行更新。
我正在保存对ImageQt.ImageQt
(self.imgQ
)的引用,否则它将崩溃。
据我了解,您正在QGraphicsScene
每次迭代中创建更好的解决方案,以创建一次,然后调用scene.clear()
。
仅为了显示其大小和居中而缩放像素图是昂贵的,QGraphicsView.fitInView()
为此目的而制造。