0

QGraphicsScene に配置され、QGraphicsView を介して視覚化される QGraphicsItems のマウス入力イベントに接続しようとしています。私が理解していることから、これをオーバーライドするメソッドは、QGraphicsItem (またはそのサブクラスの 1 つ) から派生したクラスの dragEnterEvent です。私の試みは次のようになります。

class StaPoly(QtGui.QGraphicsPolygonItem):

    def __init__(self,*args):
        QtGui.QGraphicsPolygonItem.__init__(self,*args)
        self.setAcceptDrops(True)

    def dragEnterEvent(self,event):
        print "Enter!"        

...

    def draw(self):        
        p       = self.parent

        self.group = QtGui.QGraphicsItemGroup(scene=p.scene)

...

        for xpix in lons: 
            poly = QtGui.QPolygonF()
            poly << QtCore.QPointF(xpix-symw,ypix)
            poly << QtCore.QPointF(xpix,ypix+symh)                
            poly << QtCore.QPointF(xpix+symw,ypix)                
            poly << QtCore.QPointF(xpix,ypix-symh)                                                

            item = StaPoly(poly)
            item.setPen(QtGui.QColor(color))
            item.setBrush(QtGui.QColor(color))  
            self.group.addToGroup(item)   

上記のスニペットで、私が何をしようとしているのかが明確になることを願っています。表示は希望どおりに生成されますが、問題はありません。ただし、描画されるポリゴンは入力イベントに応答しません。dragEnterEvent() が呼び出されているという証拠は見られません。

4

1 に答える 1

0

The (partial) solution was this:

self.group.setHandlesChildEvents(False)     

This ensures that individual items handle their own events, it seems that before the group was capturing them.

I still have a problem in that my GraphicsView overrides the mouseMoveEvent, and when enabled, no events get propagated to scene items.

EDIT: And it seems the solution to this second problem was to call the base class mouseMoveEvent handler from the Views mouseMoveEvent handler e.g.

def mouseMoveEvent(self,event):
    QtGui.QGraphicsView.mouseMoveEvent(self, event)
于 2011-04-04T00:32:27.143 に答える