HelderC
1st October 2012, 13:16
Hi.
If I want to create a kind of "Ms Paint" using GraphicsView, GraphicsScene and Items, which class I have to reimplement to get mousepress and mousemove events? GraphicsView or Scene?
I tried to use Scene, but even with it in 600x500 WidthxHeight ever place that I click the coordinates are the same 0,0 and the follow code dont draw any thing, in this case, a line.
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class GraphicsScene(QGraphicsScene):
def __init__(self, parent = None):
super(GraphicsScene, self).__init__(parent)
self.setSceneRect(0, 0, 600, 500)
self.setBackgroundBrush(QColor(242, 251, 235))
def mousePressEvent(self, e):
self.pointBegin = self.pointEnd = e.pos()
self.line = QGraphicsLineItem(QLineF(self.pointBegin, self.pointEnd))
self.line.setFlags(QGraphicsItem.ItemIsMovable)
self.addItem(self.line)
def mouseMoveEvent(self, e):
self.pointEnd = e.pos()
self.line.setLine(QLineF(self.pointBegin, self.pointEnd))
def mouseReleaseEvent(self, e):
self.line.setLine(QLineF(self.pointBegin, self.pointEnd))
If I want to create a kind of "Ms Paint" using GraphicsView, GraphicsScene and Items, which class I have to reimplement to get mousepress and mousemove events? GraphicsView or Scene?
I tried to use Scene, but even with it in 600x500 WidthxHeight ever place that I click the coordinates are the same 0,0 and the follow code dont draw any thing, in this case, a line.
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class GraphicsScene(QGraphicsScene):
def __init__(self, parent = None):
super(GraphicsScene, self).__init__(parent)
self.setSceneRect(0, 0, 600, 500)
self.setBackgroundBrush(QColor(242, 251, 235))
def mousePressEvent(self, e):
self.pointBegin = self.pointEnd = e.pos()
self.line = QGraphicsLineItem(QLineF(self.pointBegin, self.pointEnd))
self.line.setFlags(QGraphicsItem.ItemIsMovable)
self.addItem(self.line)
def mouseMoveEvent(self, e):
self.pointEnd = e.pos()
self.line.setLine(QLineF(self.pointBegin, self.pointEnd))
def mouseReleaseEvent(self, e):
self.line.setLine(QLineF(self.pointBegin, self.pointEnd))