Massimo
Massimo

Reputation: 966

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:

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

Upvotes: 1

Views: 1482

Answers (2)

Massimo
Massimo

Reputation: 966

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)

Upvotes: 1

a_manthey_67
a_manthey_67

Reputation: 4286

you need an eventfilter in 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)

and install him at the end of the constructor of MyView:

self.installEventFilter(self)

Upvotes: 2

Related Questions