You should look into the QGraphicsView instead of what you are doing, it has this all built in already.
http://doc.qt.nokia.com/4.7-snapshot/qgraphicsview.html
http://doc.qt.nokia.com/4.7-snapshot/qgraphicsscene.html
from PyQt4 import QtGui, QtCore
class MyFrame(QtGui.QGraphicsView):
def __init__( self, parent = None ):
super(MyFrame, self).__init__(parent)
self.setScene(QtGui.QGraphicsScene())
# add some items
x = 0
y = 0
w = 45
h = 45
pen = QtGui.QPen(QtGui.QColor(QtCore.Qt.green))
brush = QtGui.QBrush(pen.color().darker(150))
item = self.scene().addEllipse(x, y, w, h, pen, brush)
item.setFlag(QtGui.QGraphicsItem.ItemIsMovable)
if ( __name__ == '__main__' ):
app = QtGui.QApplication([])
f = MyFrame()
f.show()
app.exec_()
Edit: Showing how to create a QPainterPath
from PyQt4 import QtGui, QtCore
class MyFrame(QtGui.QGraphicsView):
def __init__( self, parent = None ):
super(MyFrame, self).__init__(parent)
scene = QtGui.QGraphicsScene()
self.setScene(scene)
# add some items
x = 0
y = 0
w = 45
h = 45
pen = QtGui.QPen(QtGui.QColor(QtCore.Qt.green))
brush = QtGui.QBrush(pen.color().darker(150))
item = scene.addEllipse(x, y, w, h, pen, brush)
item.setFlag(QtGui.QGraphicsItem.ItemIsMovable)
# create an open path
path = QtGui.QPainterPath()
path.moveTo(-w, -h)
path.lineTo(-w, h)
path.lineTo(w, h)
path.lineTo(w, -h)
clr = QtGui.QColor('blue')
clr.setAlpha(120)
brush = QtGui.QBrush(clr)
pen = QtGui.QPen(QtCore.Qt.NoPen)
fill_item = scene.addRect(-w, y, w*2, h, pen, brush)
path_item = scene.addPath(path)
if ( __name__ == '__main__' ):
app = QtGui.QApplication([])
f = MyFrame()
f.show()
app.exec_()
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…