pyqt QGraphicsView鼠标事件麻烦

pyqt QGraphicsView mouse events trouble

我正在编写一个 MDI 应用程序,每个选项卡上都有 QGraphicsView。 我添加项目,移动它们并且有一个组。但是也存在一些问题,比如:

可能所有这些操作都没有现成的解决方案 zalezaniya 在鼠标事件中,但我找到的所有教训 - 说只有鼠标事件会拯救我

如何让 QGraphicsView 捕捉鼠标事件?

import os
import sys
import sip
import maya.OpenMayaUI as mui
from PyQt4.QtCore import *
from PyQt4.QtGui import *

#----------------------------------------------------------------------
def getMayaWindow():
    ptr = mui.MQtUtil.mainWindow()
    return sip.wrapinstance(long(ptr), QObject)

#----------------------------------------------------------------------
#----------------------------------------------------------------------
class MainForm(QMainWindow):
    def __init__(self):
        super(MainForm, self).__init__(getMayaWindow())
        self.setGeometry(50,50,600,600)

        widget = QWidget()
        self.setCentralWidget(widget)
        layout = QGridLayout()
        widget.setLayout(layout)

        mdiArea = QMdiArea()
        layout.addWidget(mdiArea)

        newItem1 = vrayRectWidget(mdiArea, 'aaaa')
        newItem2 = vrayRectWidget(mdiArea, 'bbbb')

        newItem1.setMouseTracking(True)
        newItem2.setMouseTracking(True)

#----------------------------------------------------------------------
#----------------------------------------------------------------------
class vrayRectWidget(QMdiSubWindow):
    def __init__(self, parent, name):
        super(vrayRectWidget, self).__init__(parent)
        self.setAttribute(Qt.WA_DeleteOnClose)
        self.setWindowTitle(name)

        self.view = MyView()
        self.view.setMouseTracking(True)
        self.setWidget(self.view)


#----------------------------------------------------------------------
#----------------------------------------------------------------------
class MyView(QGraphicsView):
    def __init__(self):
        QGraphicsView.__init__(self)

        self.setGeometry(QRect(100, 100, 600, 400))
        self.setDragMode(QGraphicsView.RubberBandDrag)
        self.setRubberBandSelectionMode(Qt.IntersectsItemShape)
        self.setMouseTracking(True)

        self.setRenderHints(QPainter.Antialiasing | QPainter.SmoothPixmapTransform)

        self.scene = QGraphicsScene(self)
        self.scene.setSceneRect(QRectF())
        self.setScene(self.scene)
        self.setInteractive(True)

        for i in range(5):
            item = QGraphicsEllipseItem(i*75, 10, 60, 40)

            item.setFlag(QGraphicsItem.ItemIsMovable, True)
            item.setFlag(QGraphicsItem.ItemIsSelectable, True)
            self.scene.addItem(item)

    def mousePressEvent(self, event):
        print('mousePressEvent')

#----------------------------------------------------------------------
#----------------------------------------------------------------------
# window
def cacheWnd():
    wnd = MainForm()
    wnd.show()

cacheWnd()

您需要在 MyView 中使用事件过滤器:

def eventFilter(self,obj,event):
    if obj == self and event.type() == QtCore.QEvent.MouseButtonPress:      # see http://doc.qt.io/qt-5/qevent.html
    # alternatively use QtCore.QEvent.GraphicsSceneMousePress
        print('mousePressEvent')
        return True
    return QtWidgets.QGraphicsView.eventFilter(self,obj,event)

并在MyView的构造函数末尾安装他:

self.installEventFilter(self)

鼠标事件问题已解决。

我开始了一个新的class基于QGraphicsScene,它处理所有的鼠标事件:

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

    def mouseReleaseEvent(self, event):
        print('mouseReleaseEvent')
        return QGraphicsScene.mouseReleaseEvent(self, event)