Home > other >  Pyqt5 draw a straight line when how to realize real-time dynamic display mouse line, but only to rel
Pyqt5 draw a straight line when how to realize real-time dynamic display mouse line, but only to rel

Time:12-07

Recently started learning pyqt5 drawing function, but don't know how to realize the dynamic display of drawing with the mouse in the straight line, but only when the mouse to loosen to draw, join the mouse time to refresh and can cause a large number of ghosting, mouse movement time remove the refresh command will cause no PIC to loosen the mouse suddenly appeared image drawing process, in addition to the double buffer technique and also seems to be able to solve the problem, in simple terms, is how to draw a line in pyqt5 to realize the function of the rubber band in MFC,
Now write specific code is as follows, hope you guys help me answer the,
 

The from PyQt5. QtCore import *
The from PyQt5. QtGui import *
The from PyQt5. QtWidgets import *


The class PaintBoard (QWidget) :
Def __init__ (self, parent=None) :
Super (PaintBoard, self) __init__ (parent)
Self. InitData ()
Self. InitView ()

Def initData (self) :
Self. Size=QSize (600, 900)
Self. Board=QPixmap (self. The size)
The self. The board. The fill (Qt. White) # sketchpad filling to white
Self. IsEmpty=True # initial default empty sketchpad
Self. LastPoint=QPoint (0, 0)
The self. The endPoint=QPoint (0, 0)
# auxiliary canvas
Self. TempBoard=QPixmap ()
Are self. IsDrawing=False # symbol drawing

Def initView (self) :
Pass

Def paintEvent (self, event) :
Painter=QPainter (self)
X=self. LastPoint. (x)
Y=self. LastPoint. (y)
W=self. The endPoint. (x) - x
H=the self. The endPoint. (y) - y
If self. IsDrawing:
Self. TempBoard=self. Board
Pen=QPainter (self. TempBoard)
Pen. DrawRect (x, y, w, h)
Painter. DrawPixmap (0, 0, the self. TempBoard)
The else:
Pen=QPainter (self. Board)
Pen. DrawRect (x, y, w, h)
Painter. DrawPixmap (0, 0, the self. Board)

Def mousePressEvent (self, event) :
# by pressing the left mouse button
If the event. The button ()==Qt. LeftButton:
The self. The lastPoint=event. The pos ()
The self. The endPoint=self. LastPoint
Self. IsDrawing=True

Def mouseReleaseEvent (self, event) :
# release the left mouse button
If the event. The button ()==Qt. LeftButton:
The self. The endPoint=event. Pos ()
# redraw
The self. The update ()
Self. IsDrawing=False

Def mouseMoveEvent (self, event) :
If the event. The buttons () and Qt. LeftButton:
The self. The endPoint=event. Pos ()
The self. The update ()
  • Related