Reputation: 966
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()
Upvotes: 1
Views: 1482
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
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