当 QGraphicsScene 项目在它的子项目后面时,我希望鼠标抓取器检查后面的项目,如果第一个没有被抓住,然后抓住最上面的项目。
示例代码:
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_())