2013-01-22 53 views
1

所以我有一個QraphicsScene與QGraphicsPolygonItem,我標記爲可移動的。我也重寫MousePressEvent。我的代碼片段現在。MousePressEvent捕獲信號不必要 - PyQt4

def mousePressEvent(self , e): 
    self.endx = e.x() 
    self.endy = e.y() 
    if self.sender == 1: 
     self.LineChange(self.endx , self.endy) 

#... 

def CreateFun(self): 
    poly = QtGui.QPolygonF([QtCore.QPointF(100 , 100) , QtCore.QPointF(100 , 200) , QtCore.QPointF(200 , 200)]) 
    self.polygon = QtGui.QGraphicsPolygonItem(poly) 
    self.scene.addItem(self.polygon) 
    self.polygon.setFlag(QtGui.QGraphicsItem.ItemIsMovable) 

但是多邊形不移動。當我註釋掉MousePressEvent時,它運行良好。我的猜測是MousePressEvent在PolygonItem之前捕獲它。

而上述函數來自一個從QtGui.QGraphicsView繼承的類。有什麼建議麼?

+0

很難不完整代碼診斷。 – Avaris

+0

這是我的代碼,直到現在。我只是想讓用戶界面有一個直角三角形,可以移動,角度可以編輯。 http://pastebin.com/m3SLcqKK @Avaris – Manoj

+0

你能查看我的代碼嗎? @Avaris – Manoj

回答

1

您應該調用mousePressEvent的基本實現。 QGraphicsView通常會將這些點擊傳遞給可能使用它們的其他項目。如果你不打電話給基礎實施,你基本上是在「陷印」點擊。

修改您的mousePressEvent如下:

def mousePressEvent(self , e): 
    self.endx = e.x() 
    self.endy = e.y() 
    if self.sender == 1: 
     self.LineChange(self.endx , self.endy) 
    # let the base implementation do its thing 
    super(Palette, self).mousePressEvent(e) 
+0

再次感謝。我還有一個問題。假設我有一個QPolygonF項目,有4個角落,傳遞給QGraphicsPolygonItem,QGraphicsPolygonItem又被設置爲QGraohicsScene。我想擴展或基本上旋轉多邊形,根據用戶點擊的位置,我一直試圖找到語法,但無法。有什麼建議麼? – Manoj

+0

@Manoj:旋轉很簡單。 ['setRotation'](http://doc.qt.digia.com/qt/qgraphicsitem.html#setRotation)就是您所需要的。擴展取決於你想要的。最直接的方法是計算和重新設置'QPolygonF'。或者你可以創建一個'QTransform'並將其應用於'setTransform'。 – Avaris

+0

我想我混淆的語法,你可以幫忙把它整理出來,http://pastebin.com/ULy0aTVM – Manoj