您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在QGraphicsScene中显示图像

在QGraphicsScene中显示图像

没有循环代码和有效示例,很难确定您的问题。但是我有类似的测试应用程序,希望对您有所帮助。

#!/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.ImageQtself.imgQ)的引用,否则它将崩溃。

据我了解,您正在QGraphicsScene每次迭代中创建更好的解决方案,以创建一次,然后调用scene.clear()

仅为了显示其大小和居中而缩放像素图是昂贵的,QGraphicsView.fitInView()为此目的而制造。

CSS 2022/1/1 18:35:01 有558人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶