I do vector drawing.
The essence of the algorithm:
Algorithm:
- When a user draws, his scribbles are written into the SVG file using the SvgWriter library.
- After that, the QSvgRenderer should pull out the data from the file and display all of it on a QImage, which will be painted on a QPainter in a paintEvent.
The problem is that QSvgRenderer processes the file only when the program is started, i.e. during drawing on QPainter nothing is drawn.
Here is the source code:
import sys from PyQt5.QtWidgets import QWidget, QApplication, QPushButton from PyQt5.QtGui import QPainter, QPainterPath, QColor, QPen, QImage, QIcon from PyQt5.QtSvg import QSvgWidget, QSvgRenderer from PyQt5.QtCore import QSize import svgwrite class Window(QWidget): def __init__(self): super().__init__() self.last_x = 0 #last point x pos self.last_y = 0#last point y pos self.itr = 0 #iteration of points self.itr_step = 5 self.pen = QPen(QColor(10,10,16), 2, cap=32) self.init_UI() def init_UI(self): self.rend = QSvgRenderer('drawing.svg') self.dwg = svgwrite.Drawing('drawing.svg', size=('600px', '600px')) self.dwg.add(self.dwg.line((0, 0), (10, 0), stroke=svgwrite.rgb(10, 10, 16, '%'), stroke_linecap="round")) self.dwg.save() self.cnv = QImage(2560, 1440, QImage.Format_ARGB32_Premultiplied) self.setGeometry(729, 410, 600, 600) #TODO change size and start pos before relize self.setWindowTitle('SmartWrite') def paintEvent(self, event): qp = QPainter() qp.begin(self) #qp.drawImage(0, 0, self.cnv) #self.dwg.save() self.rend.render(qp) qp.end() def draw(self, x, y): painter = QPainter() painter.begin(self.cnv) painter.setPen(self.pen) painter.drawLine(self.last_x,self.last_y, x, y) self.dwg.add(self.dwg.line((self.last_x,self.last_y), (x, y), stroke=svgwrite.rgb(10, 10, 16, '%'), stroke_linecap='round', stroke_width=5)) self.dwg.save() print(x, y) painter.end() self.update() self.last_x = x self.last_y = y def mousePressEvent(self, event): self.last_x = event.x() self.last_y = event.y() self.draw(event.x(), event.y()) def mouseMoveEvent(self, event): self.itr += 1 if not self.itr % self.itr_step: self.draw(event.x(), event.y()) if __name__ == '__main__': app = QApplication(sys.argv) w = Window() w.show() sys.exit(app.exec_()) 
svgwriteis a crutch because You can draw svg using QSvgGenerator . He also knows how through QPainter :) - gil9red