0
我有PySide的問題(我在編程方面仍然是菜鳥)。我創建了名爲RBNode的自定義QGraphicsItem。它應該停留在mouseReleaseEvent發生的位置。我不知道爲什麼,但是當我試圖不止一次地移動RBNode類的實例時,這個實例的位置重置。我怎樣才能避免重置職位? 在此先感謝您的幫助。PySide:可移動項目在鼠標事件中的位置重置
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PySide.QtGui import *
from PySide.QtCore import *
class RBNode(QGraphicsItem):
def __init__(self, factorView = None):
super(RBNode, self).__init__()
self.factor = factorView
self.pressed = False
self.x = self.pos().x()
self.y = self.pos().y()
self.setFlag(QGraphicsItem.ItemIsMovable)
def boundingRect(self):
return QRectF(-50,-50,100,100)
def paint(self, painter, option, widget):
rect = QRectF(-50,-50,100,100)
if self.pressed:
painter.setBrush(Qt.red)
else:
painter.setBrush(Qt.darkGray)
painter.drawEllipse(rect)
def mousePressEvent(self, event):
self.pressed = True
self.update()
QGraphicsItem.mousePressEvent(event)
def mouseReleaseEvent(self, event):
self.pressed = False
self.update()
QGraphicsItem.mouseReleaseEvent(event)
class RBGraphicView(QGraphicsView):
def __init__(self):
super(RBGraphicView, self).__init__()
self.factorView = 1
self.initScene()
self.initGui()
def initGui(self):
self.setWindowTitle("A Simple Animation")
self.show()
def initScene(self):
self.rbScene = QGraphicsScene(self)
self.rbAddItem(self.rbScene)
self.setScene(self.rbScene)
def rbAddItem(self, scene):
rbNode1 = RBNode(self.factorView)
rbNode1.setPos(100,100)
scene.addItem(rbNode1)
if __name__ == '__main__':
try:
myApp = QApplication(sys.argv)
myView = RBGraphicView()
myApp.exec_()
sys.exit(0)
except NameError:
print("Name Error:", sys.exc_info()[1])
except SystemExit:
print("Closing Window...")
except Exception:
print(sys.exc_info()[1])