PDA

View Full Version : Draw rect when mouse is pressed



crocpulsar
19th March 2009, 04:51
Hi,
Here is a piece of qt4 code written in python. Basically, a rectangle is painted when I press the mouse button. But the problem is the qgraphicsview will re-position the view, so the item painted is not under where the mouse is pressed. How do I solve this?


from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys

class MapView(QGraphicsView):

def __init__(self, parent = None):
QGraphicsView.__init__(self, parent)

def mousePressEvent(self, event):
mousePos = self.mapToScene(event.pos())
self.scene().addRect(mousePos.x(), mousePos.y(), 10, 10)

app = QApplication(sys.argv)
scene = QGraphicsScene()
view = MapView()
view.setScene(scene)
view.setFixedSize(400, 300)
view.show()
sys.exit(app.exec_())