接着上文:绘制基础图形函数已经全部清楚,然后进阶鼠标拖画。
鼠标拖画,需要用到鼠标事件
鼠标事件:
与C++Qt一样,鼠标事件,需要重载鼠标事件函数如下:
def mousePressEvent(self, e): print("鼠标按下事件")
- 1
- 2
def mouseReleaseEvent(self, e): print("鼠标释放事件") if e.button() == Qt.LeftButton: print("左键") elif e.button() == Qt.RightButton: print("右键") elif e.button() == Qt.MidButton: print("点击滚轮")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
def mouseMoveEvent(self, e): print("鼠标移动事件")
- 1
- 2
鼠标绘制图形:
点击相应按钮,鼠标拖动画出对应图形:
思路:
创建对应List用来存储鼠标坐标点。
鼠标左键点击绘制,右键清空
画点:
创建List
self.Point_list = []
- 1
绘制点
if self.Draw == "Point": len_point_list = len(self.Point_list)/2 for i in range(int(len_point_list)): #for循环绘制所有坐标点 painter.drawPoint(self.Point_list[i*2],self.Point_list[i*2+1]) print("DrawPoint")
- 1
- 2
- 3
- 4
- 5
鼠标按下事件:存储坐标点
if self.Draw =="Point": self.Point_list.append(e.x()) self.Point_list.append(e.y()) self.update()#更新界面
- 1
- 2
- 3
- 4
所有绘制:
List创建:
其中点、多边行的绘制,皆为不确定List,所以定义为不定长空List。
其他绘制均为定长List
self.Line_list = [0,0,0,0] self.Point_list = [] self.Elipse_list = [0,0,0,0] self.Rectangle_list = [0,0,0,0] self.Polygon_list = [] self.Pie_list = [0,0,0,0] self.Arc_list = [0,0,0,0]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
绘制函数:
绘制多边形部分,只有鼠标左键按下三次,记录三个坐标点及以上才能绘制多边形
def paintEvent(self, QPaintEvent): painter = QPainter(self) painter.setPen(QColor(166,66,250)) painter.begin(self) if self.Draw == "Line": painter.drawLine(self.Line_list[0],self.Line_list[1],self.Line_list[2],self.Line_list[3]) print("DrawLine") elif self.Draw == "Point": len_point_list = len(self.Point_list)/2 for i in range(int(len_point_list)): painter.drawPoint(self.Point_list[i*2],self.Point_list[i*2+1]) print("DrawPoint") elif self.Draw == "Elipse": painter.drawEllipse(self.Elipse_list[0],self.Elipse_list[1],self.Elipse_list[2],self.Elipse_list[3]) print("DrawElipse") elif self.Draw == "Rectangle": painter.drawRect(self.Rectangle_list[0],self.Rectangle_list[1],self.Rectangle_list[2],self.Rectangle_list[3]) print("DrawRectangle") elif self.Draw == "Text": painter.drawText(120,120,"文字") print("DrawText") elif self.Draw == "Polygon": polygon = QPolygon() if len(self.Polygon_list) >= 6: polygon.setPoints(self.Polygon_list) print(len(self.Polygon_list)) painter.drawPolygon(polygon) print("DrawPolygon") elif self.Draw == "Pie": painter.drawPie(self.Pie_list[0],self.Pie_list[1],self.Pie_list[2],self.Pie_list[3],0*16,120*16) print("DrawPie") elif self.Draw =="Arc": painter.drawArc(self.Arc_list[0],self.Arc_list[1],self.Arc_list[2],self.Arc_list[3],30*16,120*16) print("DrawArc") elif self.Draw == "Path": path = QPainterPath() path.addRect(100,100,100,100) path.addEllipse(150,150,60,80) painter.setBrush(Qt.blue) #path.setFillRule(Qt.WindingFill) path.setFillRule(Qt.OddEvenFill) painter.drawPath(path) print("DrawPath") painter.end()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
鼠标按下事件:
def mousePressEvent(self, e): if self.Draw == "Line" : self.Line_list[0] = e.x() self.Line_list[1] = e.y() print("start",self.Line_list[0],self.Line_list[1]) elif self.Draw =="Point": self.Point_list.append(e.x()) self.Point_list.append(e.y()) self.update() elif self.Draw == "Elipse": self.Elipse_list[0]=e.x() self.Elipse_list[1]=e.y() elif self.Draw == "Rectangle": self.Rectangle_list[0] = e.x() self.Rectangle_list[1] = e.y() elif self.Draw == "Polygon": self.Polygon_list.append(e.x()) self.Polygon_list.append(e.y()) print(self.Polygon_list) self.update() elif self.Draw == "Pie": self.Pie_list[0] = e.x() self.Pie_list[1] = e.y() elif self.Draw == "Arc": self.Arc_list[0] = e.x() self.Arc_list[1] = e.y() if e.button() == Qt.RightButton: self.Point_list.clear() self.Elipse_list = [0,0,0,0] self.Rectangle_list = [0,0,0,0] self.Polygon_list.clear() self.Pie_list = [0,0,0,0] self.Arc_list = [0,0,0,0] self.update()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
鼠标移动事件:
除画线以为,其他第二个坐标点均为相对坐标点,长,宽或者半径
def mouseMoveEvent(self, e): if self.Draw == "Line" : self.Line_list[2] = e.x() self.Line_list[3] = e.y() print("end",self.Line_list[2],self.Line_list[3]) self.update() elif self.Draw == "Elipse": self.Elipse_list[2] = e.x()-self.Elipse_list[0] self.Elipse_list[3] = e.y()-self.Elipse_list[1] self.update() print("Radius",self.Elipse_list[2],self.Elipse_list[3]) elif self.Draw == "Rectangle": self.Rectangle_list[2] = e.x()-self.Rectangle_list[0] self.Rectangle_list[3] = e.y()-self.Rectangle_list[1] self.update() elif self.Draw == "Pie": self.Pie_list[2] = e.x() - self.Pie_list[0] self.Pie_list[3] = e.y() - self.Pie_list[1] self.update() elif self.Draw == "Arc": self.Arc_list[2] = e.x() - self.Arc_list[0] self.Arc_list[3] = e.y() - self.Arc_list[1] self.update()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
源代码如下:
源代码
文章来源: blog.csdn.net,作者:何其不顾四月天,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/u011218356/article/details/80954066