当我尝试刷新嵌入在QGraphicsScene中的Canvas时,如何使我的代码更快?

时间:2019-07-24 15:20:53

标签: python pyside qgraphicsscene

我必须使用QGraphicsScene来显示我的画布,并且需要刷新它。

我在python上工作了几个月,所以实际上找不到解决方案。

因此,为实现这一点,在尝试使用线程之前,我使用了QTimer,但效果并不理想。

from PySide import QtCore, QtGui
import sys
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg
import os
import dicom


fig = None
figs = []
axes = []
for i in range(12):
    fig = Figure()
    fig.set_facecolor('black')
    axes.append(fig.add_subplot(111))
    figs.append(fig)

img_list = []
slice_location = []
img_path = "dicom image path directory"
for filename in os.listdir(img_path):  # I store the image in a list
    try:
        data = dicom.read_file("" + img_path + "\\" + filename + "")
        img_list.append(data.pixel_array)
    except:
        break


class Main(QtGui.QFrame): # main frame
    def __init__(self, app, label, *args, **kwargs):
        super(Main, self).__init__()
        self.thread = Thread()
        self.thread.start()
        layout = QtGui.QGridLayout(self)
        names = ['1.1', '1.2', '1.3', '1.4', '2.1', '2.2', '2.3', '2.4', '3.1', '3.2', '3.3', '3.4']
        positions = [(i, j) for i in range(3) for j in range(4)]
        self.i = 0
        for position, name in zip(positions, names):
            layout.setSpacing(1)
            self.canvas = MyView(self.i)
            self.i += 1
            layout.addWidget(self.canvas, *position)


class MyView(QtGui.QGraphicsView): # sub frame where the image are goint to be shown with zoom and pan functions
    def __init__(self, i):
        QtGui.QGraphicsView.__init__(self)
        scene = QtGui.QGraphicsScene(self)
        self.scene = scene
        self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
        self.figure = figs[i]
        canvas = FigureCanvasQTAgg(self.figure)
        scene.addWidget(canvas)
        self.setScene(scene)
        self.m_originX = 0
        self.m_originY = 0
        self.rightMousePressed = None

    def wheelEvent(self, event, *args, **kwargs):
        zoom_in = 1.15
        zoom_out = 1.0 / zoom_in
        if event.delta() > 0:
            self.scale(zoom_in, zoom_in)
        else:
            self.scale(zoom_out, zoom_out)

    def mousePressEvent(self, event, *args, **kwargs):
        if event.button() == QtCore.Qt.MidButton:
            self.rightMousePressed = True
            self.m_originX = event.x()
            self.m_originY = event.y()
            self.setCursor(QtCore.Qt.ClosedHandCursor)

    def mouseReleaseEvent(self, event, *args, **kwargs):
        if event.button() == QtCore.Qt.MidButton:
            self.rightMousePressed = False
            self.setCursor(QtCore.Qt.ArrowCursor)

    def mouseMoveEvent(self, event, *args, **kwargs):
        if self.rightMousePressed:
            self.horizontalScrollBar().setValue(self.horizontalScrollBar().value() - (event.x() - self.m_originX))
            self.verticalScrollBar().setValue(self.verticalScrollBar().value() - (event.y() - self.m_originY))
            self.m_originX = event.x()
            self.m_originY = event.y()


class Thread(QtCore.QThread): # thread to redraw canvas
    def __init__(self):
        super(Thread, self).__init__()
        self.axes = axes
        self.img = img_list
        self.i = 0
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.loop0)
        self.timer.start()

    def loop0(self):
        for i in range(12):
            self.axes[i].imshow(self.img[self.i])
            self.axes[i].figure.canvas.draw()
            self.axes[i].cla()
            self.i += 1


app = QtGui.QApplication(sys.argv)
widget = Main(QtGui.QWidget, "test")
widget.show()
app.exec_()

0 个答案:

没有答案