I’m trying to make a very basic image creator using PySide. But I’m having some troubles to draw things.
This is the portion of my script responsible for the draw area:
class Area(QWidget):
global x1
global y1
global x2
global y2
try:
x1
except NameError:
x1=0
try:
y1
except NameError:
y1=0
try:
x2
except NameError:
x2=100
try:
y2
except NameError:
y2=100
def mousePressEvent(self, me):
global x1
global y1
x1 = me.x()
y1 = me.y()
def mouseMoveEvent(self, mo):
global x2
global y2
x2 = mo.x()
y2 = mo.y()
self.update()
def paintEvent(self, pe):
global paint
paint = QPainter()
paint.begin(self)
paint.drawLine(x1,y1,x2,y2)
paint.save()
paint.restore()
blankarea = Area()
blankarea.show()
Basicaly, what the script does is just draw a line based on mouse events. However, every time the mouse click event occurs (responsible for start a line) it erases the line previously made, even after using QPainter.save() and QPainter.restore(). What could be causing this problem?
The
paintEvent()method is repainting the entire widget every time it is called. The drawing from the previous calls is completely replaced with the results of each futurepaintEvent()call.If you want the effect you are describing, you will need to accumulate the points and draw the entire sequence of them each time.
From the docs:
http://pyside.github.io/docs/pyside/PySide/QtGui/QWidget.html#PySide.QtGui.PySide.QtGui.QWidget.paintEvent