在 PyQt 中,图像不会在覆盖 QGraphicsView 的 mousePressEvent 后显示

In PyQt, image won't show after override mousePressEvent of QGraphicsView

我想写一个简单的程序来显示图片并打印被覆盖 QGraphicsViewmousePressEvent 点击的像素。

当我不覆盖 QGraphicsViewmousePressEvent 时,图像显示正常。但是当我覆盖它时,不仅位置没有显示出来,canvas 变成空白。

覆盖前:

import sys

from PyQt5 import Qt
from PyQt5 import uic

a = Qt.QApplication(sys.argv)

from untitled import Ui_Form

# class override_graphicsView (Qt.QGraphicsView):
#
#     def mousePressEvent(self, event):
#         print(event.pos())

class Image_Process(Qt.QWidget):
    def __init__(self):
        super(Image_Process, self).__init__()
        self.path = r"d:\test\winxp.jpg"  #image path

        self.new = Ui_Form()
        self.new.setupUi(self)

        # self.new.graphicsView = override_graphicsView()

        self.pixmap = Qt.QPixmap()
        self.pixmap.load(self.path)
        self.pixmap = self.pixmap.scaled(self.size(), Qt.Qt.KeepAspectRatio)

        self.graphicsPixmapItem = Qt.QGraphicsPixmapItem(self.pixmap)

        self.graphicsScene = Qt.QGraphicsScene()
        self.graphicsScene.addItem(self.graphicsPixmapItem)

        self.new.graphicsView.setScene(self.graphicsScene)


my_Qt_Program = Image_Process()

my_Qt_Program.show()
sys.exit(a.exec_())

在我取消注释这些行后,canvas变成了这个,点击后什么也没有打印。

untitled.py 是从 QtDesigner 生成的

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'untitled.ui'
#
# Created: Mon Jan 12 02:07:05 2015
#      by: PyQt5 UI code generator 5.3.2
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(451, 286)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(Form.sizePolicy().hasHeightForWidth())
        Form.setSizePolicy(sizePolicy)
        self.gridLayout = QtWidgets.QGridLayout(Form)
        self.gridLayout.setObjectName("gridLayout")
        self.graphicsView = QtWidgets.QGraphicsView(Form)
        self.graphicsView.setObjectName("graphicsView")
        self.gridLayout.addWidget(self.graphicsView, 0, 0, 1, 1)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))

Plouff的帮助下,我解决了自己的问题

首先,我应该覆盖 QGraphicsScene 而不是 QGraphicsView,并且应该为 QGraphicsScene 调用以下行来处理 mousePressEvent

super(override_graphicsScene, self).mousePressEvent(event)

修改后的代码:

import sys

from PyQt5 import Qt
from PyQt5 import uic

a = Qt.QApplication(sys.argv)

from untitled import Ui_Form


# Override like this:

class override_graphicsScene (Qt.QGraphicsScene):
    def __init__(self,parent = None):
        super(override_graphicsScene,self).__init__(parent)

    def mousePressEvent(self, event):
        super(override_graphicsScene, self).mousePressEvent(event)
        print(event.pos())

class Image_Process(Qt.QWidget):
    def __init__(self):
        super(Image_Process, self).__init__()
        self.path = r"d:\test\winxp.jpg"  #image path

        self.new = Ui_Form()
        self.new.setupUi(self)

        self.pixmap = Qt.QPixmap()
        self.pixmap.load(self.path)
        self.pixmap = self.pixmap.scaled(self.size(), Qt.Qt.KeepAspectRatio)

        self.graphicsPixmapItem = Qt.QGraphicsPixmapItem(self.pixmap)

        self.graphicsScene = override_graphicsScene(self)
        self.graphicsScene.addItem(self.graphicsPixmapItem)

        self.new.graphicsView.setScene(self.graphicsScene)

my_Qt_Program = Image_Process()

my_Qt_Program.show()
sys.exit(a.exec_())

程序运行良好。