Qt mouse event passing with scene elements

When a QGraphicsScene element is behind it as a child, I want the mouse capture to check the element behind, and then capture the top element if the first is not captured.

SAMPLE CODE:

from PySide.QtCore import * from PySide.QtGui import * class View(QGraphicsView): pass class Scene(QGraphicsScene): pass class ChildCircle(QGraphicsEllipseItem): def __init__(self, parent): super(ChildCircle, self).__init__() self.setRect(QRect(-20,-20,70,70)) self.setParentItem( parent ) def mousePressEvent(self, event): print "Circle is Pressed", event.pos() class ParentRectangle(QGraphicsRectItem): def __init__(self, scene): super(ParentRectangle, self).__init__() self.scene = scene self.setRect(QRect(0,0,20,20)) self.scene.addItem(self) circle = ChildCircle(self) def mousePressEvent(self, event): print "Rectangle PRESS", event.pos() class Window(QMainWindow): def __init__(self): QMainWindow.__init__(self) self.s = Scene() self.s.setSceneRect(-200,-100,300,300,) self.v = View(self.s) self.v.setDragMode(QGraphicsView.ScrollHandDrag) self.setCentralWidget(self.v) ParentRectangle(self.s) if __name__ == '__main__': import sys app = QApplication(sys.argv) window = Window() window.resize(300, 200) window.show() sys.exit(app.exec_()) 
0
source share
1 answer

I'm not sure I understand your question. The Qt documentation clearly states the following about the mousePressEvent method:

The mouse click event determines which element the grabber should become a mouse. If you reimplement this function, the default event will be accepted (see QEvent :: accept ()), and this element is a grabber mouse. This allows the item to receive future move, release, and double-click events. If you raise the QEvent :: ignore () event , this element will lose mouse capture , and the event will be propagated to any topmost element below.

All you need to do to decide whether to call the QEvent::ignore method or not. So, for example, if the circle object always ignores the mouse click event, the rectangle object will always be the mouse invader (if you click on the rectangle). In this code, mouse capture is the element you clicked.

 class ChildCircle(QGraphicsEllipseItem): def __init__(self, parent=None): super(ChildCircle, self).__init__(parent) self.setRect(QRect(-20,-20,70,70)) self.setFlags(QGraphicsItem.ItemIsMovable) def mousePressEvent(self, event): # Ugly way to know there are items except self if len(self.scene().items(event.scenePos())) > 1: # Propogate the event to the parent event.ignore() class ParentRectangle(QGraphicsRectItem): def __init__(self, scene, parent=None): super(ParentRectangle, self).__init__(parent) self.scene = scene self.setRect(QRect(0,0,20,20)) self.scene.addItem(self) circle = ChildCircle(self) self.setFlags(QGraphicsItem.ItemIsMovable) def mousePressEvent(self, event): pass 
+2
source

Source: https://habr.com/ru/post/945874/


All Articles