pyqt 更新 qGraphicsScene 与鼠标滚轮事件输出



我有一组图像,我希望能够滚动浏览。下面的代码在 Mac 上对我有用,这样我就可以在可见的小部件上滚动。打印"计数"会导致 python 输出中出现可见的上升或减少数字。但是,我希望使用"计数"来更新图像,我有点坚持如何。

主要问题是:如何使用鼠标滚轮函数中不断变化的变量"count",并使用它来更新 DCMViewer 中显示的图像?我希望某种信号起作用,但还没有让它工作,我被卡住了。任何帮助都非常感谢。

class DCMViewer(QGraphicsView):
    def __init__(self):
        super(DCMViewer, self).__init__()
        # Create a QGraphicsScene
        self.scene = QGraphicsScene()
        # Provide image in normalized double format
        image = np.double(dcmIm)
        image *= 255.0 / image.max()
        image = QPixmap.fromImage(qimage2ndarray.array2qimage(image))
        image = QGraphicsPixmapItem(image)
        # Add the image to the QGraphicsScene window
        self.scene.addItem(image)
        # Initiate the scene
        self.setScene(self.scene)
    # Create a mousewheelevent to scroll through images
    def wheelEvent(self, QWheelEvent):
        super(DCMViewer, self).wheelEvent(QWheelEvent)
        global count
        wheelcounter = QWheelEvent.angleDelta()
        if wheelcounter.y() / 120 == -1:
            count += 1
            if count >= 21:
                count = 21
        elif wheelcounter.y() / 120 == 1:
            count -= 1
            if count <= 0:
                count = 0
class Mainwidget(QMainWindow):
    def __init__(self):
        super(Mainwidget, self).__init__()
        # self.initUI()
        image_viewer = DCMViewer()
        self.setCentralWidget(image_viewer)
        self.showFullScreen()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Mainwidget()
    win.showFullScreen()
    sys.exit(app.exec_())

为了使项目可见,我们必须做的第一项任务是识别它,为此我们可以为每个项目建立一个 ID。我们使用 setData()data() 方法,然后我们使用 fitInView() 方法将窗口缩放到项目,这不会使项目完全可见,因为如果上面有另一个项目,我们将得到不需要的输出,为此我们使用 setZValue() ,Z 的值越高,在另一个项目之上, 您的代码不会提供可以正确测试的示例,因此请创建我自己的示例:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class DCMViewer(QGraphicsView):
    def __init__(self):
        super(DCMViewer, self).__init__()
        self.currentId = 0
        self.maxId = 0
        self.setScene(QGraphicsScene())
        directory = QStandardPaths.writableLocation(QStandardPaths.PicturesLocation)
        it = QDirIterator(directory, QDir.Files)
        while it.hasNext():
            pixmap = QPixmap(it.next())
            item = QGraphicsPixmapItem(pixmap)
            item.setData(0, self.maxId)
            self.scene().addItem(item)
            self.maxId += 1
    def scrollToItem(self, id):
        for item in self.scene().items():
            if item.data(0) == id:
                item.setZValue(1)
                self.fitInView(item)
            else:
                item.setZValue(0)
    def wheelEvent(self, event):
        wheelcounter = event.angleDelta()
        if wheelcounter.y() / 120 == -1:
            self.currentId += 1
            if self.currentId > self.maxId:
                self.currentId = self.maxId -1
        elif wheelcounter.y() / 120 == 1:
            self.currentId -= 1
            if self.currentId <= 0:
                self.currentId = 0
        self.scrollToItem(self.currentId)

class Mainwidget(QMainWindow):
    def __init__(self):
        super(Mainwidget, self).__init__()
        image_viewer = DCMViewer()
        self.setCentralWidget(image_viewer)
        self.showFullScreen()
        image_viewer.scrollToItem(0)

if __name__ == '__main__':
    app = QApplication(sys.argv)
    win = Mainwidget()
    sys.exit(app.exec_())

最新更新