如果沒有循環代碼和工作示例,很難確定您的問題。但我有類似的測試應用程序,希望它會有所幫助。
#!/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 = QVBoxLayout()
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()
。
縮放pixmap只是爲了顯示它的大小和居中價格昂貴,QGraphicsView.fitInView()
爲此目的。
這很好,謝謝。 – TimothyAWiseman 2012-03-20 20:17:17