Search code examples
pyqtmouseeventzoomingqgraphicsviewrubber-band

pyqt QGraphicsView mouse events trouble


I'm writing an MDI application with QGraphicsView on each tab. I add items, move them and there is a group. But there are a number of problems, such as:

  • a one-time allocation of objects. I can not rubber band select some objects and holding the Shift to add to the selection with other rubber band selection. this can be done for example if it is before the new allocation remember old objects and add it after the separation that was previously. but it is done through mouse events, and they do not work at all

  • necessary when you click on an object to do some action, but it is also done through mouse events ...

  • i need the mouse wheel to zoom, and then rests on the mouse events

possible for all of these actions have no ready-made solutions zalezaniya in mouse events, but all the lessons that I find - say that the only mouse events will save me

how to make a QGraphicsView to catch mouse events?

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()

Solution

  • problem with mouse events resolved.

    I started a new class based on QGraphicsScene, and it handles all mouse events:

    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)