【问题标题】:pyqt QGraphicsView mouse events troublepyqt QGraphicsView鼠标事件麻烦
【发布时间】:2015-10-12 10:30:06
【问题描述】:

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

  • 对象的一次性分配。我不能用橡皮筋选择一些对象并按住 Shift 键以添加到其他橡皮筋选择中。例如,如果在新分配之前记住旧对象并在之前的分离之后添加它,则可以这样做。但它是通过鼠标事件完成的,它们根本不起作用

  • 当你点击一个对象做一些动作时是必须的,但它也是通过鼠标事件来完成的......

  • 我需要鼠标滚轮来缩放,然后停留在鼠标事件上

所有这些动作都可能在鼠标事件中没有现成的解决方案 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()

【问题讨论】:

    标签: pyqt mouseevent zooming qgraphicsview rubber-band


    【解决方案1】:

    您需要在 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 MyView(QGraphicsView): def __init__(self): QGraphicsView.__init__ (self) self.installEventFilter(self)
    • eventFilter() 必须是 MyView 的函数
    • 我可以在我的图形视图中重现这种效果:只有在设置图形视图的宽度和高度后安装 eventfilter 才有效。所以尝试将线放在 setGeometry() 之后或构造函数的末尾。答案已编辑
    • 我尝试移动您编写的过滤器 - 它没有帮助。仍然由于某种原因它不适用于所有鼠标事件
    【解决方案2】:

    鼠标事件问题已解决。

    我创建了一个基于 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-10
      • 2012-03-15
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      • 2022-12-18
      • 1970-01-01
      相关资源
      最近更新 更多