PyQt5利用QPainter绘制各种图形的实例


Posted in Python onOctober 19, 2017

这个例子我做了好几天:

1)官网C++的源码,改写成PyQt5版本的代码,好多细节不会转化

2)网上的PyQt的例子根本运行不了

填了无数个坑,结合二者,终于能完成了一个关于绘图的东西。这个过程也掌握了很多新的知识点

【知识点】

1、关于多个点的使用

poitns = [QPoint(10, 80), QPoint(20, 10), QPoint(80, 30), QPoint(90, 70)]

请看:

# 定义多个点
   points = [QPoint(10, 80), QPoint(20, 10), QPoint(80, 30), QPoint(90, 70)]

   # ===直接使用 points 会报错!=========
   # ...
   elif self.shape == self.Points:
      painter.drawPoints(points)

   elif self.shape == self.Polyline:
      painter.drawPolyline(points)

   elif self.shape == self.Polygon:
      painter.drawPolygon(points, 4)

   # ...

   # ===把 points 用 QPolygon()包裹起来才正确!=========
   # ...
   elif self.shape == self.Points:
      painter.drawPoints(QPolygon(points))

   elif self.shape == self.Polyline:
      painter.drawPolyline(QPolygon(points))

   elif self.shape == self.Polygon:
      painter.drawPolygon(QPolygon(points), 4)

   # ...

2、在QDialog窗体中显示QWidget部件

【效果图】

PyQt5利用QPainter绘制各种图形的实例PyQt5利用QPainter绘制各种图形的实例

【资源】

//img.jbzj.com/file_images/article/201710/brick.png
//img.jbzj.com/file_images/article/201710/qt-logo.png

【代码】

import sys

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

class StockDialog(QWidget):
  def __init__(self, parent=None):
    super(StockDialog, self).__init__(parent)
    self.setWindowTitle("利用QPainter绘制各种图形")
    
    mainSplitter = QSplitter(Qt.Horizontal)
    mainSplitter.setOpaqueResize(True)
         
    frame = QFrame(mainSplitter)
    mainLayout = QGridLayout(frame)
    #mainLayout.setMargin(10)
    mainLayout.setSpacing(6)

    label1=QLabel("形状:")
    label2=QLabel("画笔线宽:")
    label3=QLabel("画笔颜色:")
    label4=QLabel("画笔风格:")
    label5=QLabel("画笔顶端:")
    label6=QLabel("画笔连接点:")
    label7=QLabel("画刷风格:")
    label8=QLabel("画刷颜色:")
  
    self.shapeComboBox = QComboBox()
    self.shapeComboBox.addItem("Line", "Line")
    self.shapeComboBox.addItem("Rectangle", "Rectangle")
    self.shapeComboBox.addItem('Rounded Rectangle','Rounded Rectangle')
    self.shapeComboBox.addItem('Ellipse','Ellipse')
    self.shapeComboBox.addItem('Pie','Pie')
    self.shapeComboBox.addItem('Chord','Chord')
    self.shapeComboBox.addItem('Path','Path')
    self.shapeComboBox.addItem('Polygon','Polygon')
    self.shapeComboBox.addItem('Polyline','Polyline')
    self.shapeComboBox.addItem('Arc','Arc')
    self.shapeComboBox.addItem('Points','Points')
    self.shapeComboBox.addItem('Text','Text')
    self.shapeComboBox.addItem('Pixmap','Pixmap')
    
    self.widthSpinBox = QSpinBox()
    self.widthSpinBox.setRange(0,20)
    
    self.penColorFrame = QFrame()
    self.penColorFrame.setAutoFillBackground(True)
    self.penColorFrame.setPalette(QPalette(Qt.blue))
    self.penColorPushButton = QPushButton("更改")
    
    self.penStyleComboBox = QComboBox()
    self.penStyleComboBox.addItem("Solid",Qt.SolidLine)
    self.penStyleComboBox.addItem('Dash', Qt.DashLine)
    self.penStyleComboBox.addItem('Dot', Qt.DotLine)
    self.penStyleComboBox.addItem('Dash Dot', Qt.DashDotLine)
    self.penStyleComboBox.addItem('Dash Dot Dot', Qt.DashDotDotLine)
    self.penStyleComboBox.addItem('None', Qt.NoPen)
    
    self.penCapComboBox = QComboBox()
    self.penCapComboBox.addItem("Flat",Qt.FlatCap)
    self.penCapComboBox.addItem('Square', Qt.SquareCap)
    self.penCapComboBox.addItem('Round', Qt.RoundCap)
    
    self.penJoinComboBox = QComboBox()
    self.penJoinComboBox.addItem("Miter",Qt.MiterJoin)
    self.penJoinComboBox.addItem('Bebel', Qt.BevelJoin)
    self.penJoinComboBox.addItem('Round', Qt.RoundJoin)
    
    self.brushStyleComboBox = QComboBox()
    self.brushStyleComboBox.addItem("Linear Gradient",Qt.LinearGradientPattern)
    self.brushStyleComboBox.addItem('Radial Gradient', Qt.RadialGradientPattern)
    self.brushStyleComboBox.addItem('Conical Gradient', Qt.ConicalGradientPattern)
    self.brushStyleComboBox.addItem('Texture', Qt.TexturePattern)
    self.brushStyleComboBox.addItem('Solid', Qt.SolidPattern)
    self.brushStyleComboBox.addItem('Horizontal', Qt.HorPattern)
    self.brushStyleComboBox.addItem('Vertical', Qt.VerPattern)
    self.brushStyleComboBox.addItem('Cross', Qt.CrossPattern)
    self.brushStyleComboBox.addItem('Backward Diagonal', Qt.BDiagPattern)
    self.brushStyleComboBox.addItem('Forward Diagonal', Qt.FDiagPattern)
    self.brushStyleComboBox.addItem('Diagonal Cross', Qt.DiagCrossPattern)
    self.brushStyleComboBox.addItem('Dense 1', Qt.Dense1Pattern)
    self.brushStyleComboBox.addItem('Dense 2', Qt.Dense2Pattern)
    self.brushStyleComboBox.addItem('Dense 3', Qt.Dense3Pattern)
    self.brushStyleComboBox.addItem('Dense 4', Qt.Dense4Pattern)
    self.brushStyleComboBox.addItem('Dense 5', Qt.Dense5Pattern)
    self.brushStyleComboBox.addItem('Dense 6', Qt.Dense6Pattern)
    self.brushStyleComboBox.addItem('Dense 7', Qt.Dense7Pattern)
    self.brushStyleComboBox.addItem('None', Qt.NoBrush)
    
    self.brushColorFrame = QFrame()
    self.brushColorFrame.setAutoFillBackground(True)
    self.brushColorFrame.setPalette(QPalette(Qt.green))
    self.brushColorPushButton = QPushButton("更改")
                               
    labelCol=0
    contentCol=1
    
    #建立布局
    mainLayout.addWidget(label1,1,labelCol)
    mainLayout.addWidget(self.shapeComboBox,1,contentCol)
    mainLayout.addWidget(label2,2,labelCol)
    mainLayout.addWidget(self.widthSpinBox,2,contentCol)
    mainLayout.addWidget(label3,4,labelCol)
    mainLayout.addWidget(self.penColorFrame,4,contentCol)
    mainLayout.addWidget(self.penColorPushButton,4,3)
    mainLayout.addWidget(label4,6,labelCol)
    mainLayout.addWidget(self.penStyleComboBox,6,contentCol)
    mainLayout.addWidget(label5,8,labelCol)
    mainLayout.addWidget(self.penCapComboBox,8,contentCol)
    mainLayout.addWidget(label6,10,labelCol)
    mainLayout.addWidget(self.penJoinComboBox,10,contentCol)
    mainLayout.addWidget(label7,12,labelCol)
    mainLayout.addWidget(self.brushStyleComboBox,12,contentCol)
    mainLayout.addWidget(label8,14,labelCol)
    mainLayout.addWidget(self.brushColorFrame,14,contentCol)
    mainLayout.addWidget(self.brushColorPushButton,14,3)
    mainSplitter1 = QSplitter(Qt.Horizontal)
    mainSplitter1.setOpaqueResize(True)
    
    stack1 = QStackedWidget()
    stack1.setFrameStyle(QFrame.Panel|QFrame.Raised)
    self.area = PaintArea()
    stack1.addWidget(self.area)    
    frame1 = QFrame(mainSplitter1)
    mainLayout1 = QVBoxLayout(frame1)
    #mainLayout1.setMargin(10)
    mainLayout1.setSpacing(6)
    mainLayout1.addWidget(stack1)

    layout = QGridLayout(self)
    layout.addWidget(mainSplitter1,0,0)
    layout.addWidget(mainSplitter,0,1)
    self.setLayout(layout)
    
    #信号和槽函数
    self.shapeComboBox.activated.connect(self.slotShape)
    self.widthSpinBox.valueChanged.connect(self.slotPenWidth)
    self.penColorPushButton.clicked.connect(self.slotPenColor)
    self.penStyleComboBox.activated.connect(self.slotPenStyle)
    self.penCapComboBox.activated.connect(self.slotPenCap)
    self.penJoinComboBox.activated.connect(self.slotPenJoin)
    self.brushStyleComboBox.activated.connect(self.slotBrush)
    self.brushColorPushButton.clicked.connect(self.slotBrushColor)
    
    self.slotShape(self.shapeComboBox.currentIndex())
    self.slotPenWidth(self.widthSpinBox.value())
    self.slotBrush(self.brushStyleComboBox.currentIndex())    
    
  def slotShape(self,value):
    shape = self.area.Shape[value]
    self.area.setShape(shape)
  
  def slotPenWidth(self,value):
    color = self.penColorFrame.palette().color(QPalette.Window)
    style = Qt.PenStyle(self.penStyleComboBox.itemData(self.penStyleComboBox.currentIndex(),Qt.UserRole))
    cap = Qt.PenCapStyle(self.penCapComboBox.itemData(self.penCapComboBox.currentIndex(),Qt.UserRole))
    join = Qt.PenJoinStyle(self.penJoinComboBox.itemData(self.penJoinComboBox.currentIndex(),Qt.UserRole))
    self.area.setPen(QPen(color,value,style,cap,join))
  
  def slotPenStyle(self,value):
    self.slotPenWidth(value)
  
  def slotPenCap(self,value):
    self.slotPenWidth(value)
  
  def slotPenJoin(self,value):
    self.slotPenWidth(value)
  
  def slotPenColor(self):
    color = QColorDialog.getColor(Qt.blue)
    self.penColorFrame.setPalette(QPalette(color))
    self.area.setPen(QPen(color))
    
  def slotBrushColor(self):
    color = QColorDialog.getColor(Qt.blue)
    self.brushColorFrame.setPalette(QPalette(color))
    self.slotBrush(self.brushStyleComboBox.currentIndex())
  
  def slotBrush(self,value):
    color = self.brushColorFrame.palette().color(QPalette.Window)
    style = Qt.BrushStyle(self.brushStyleComboBox.itemData(value,Qt.UserRole))
    
    if(style == Qt.LinearGradientPattern):
      linearGradient = QLinearGradient(0,0,400,400)
      linearGradient.setColorAt(0.0,Qt.white)
      linearGradient.setColorAt(0.2,color)
      linearGradient.setColorAt(1.0,Qt.black)
      self.area.setBrush(linearGradient)
    elif style ==Qt.RadialGradientPattern:
      radialGradient = QRadialGradient(200, 200, 80, 70, 70);
      radialGradient.setColorAt(0.0, Qt.white)
      radialGradient.setColorAt(0.2, Qt.green)
      radialGradient.setColorAt(1.0, Qt.black)
      self.area.setBrush(radialGradient)
    elif(style == Qt.ConicalGradientPattern):
      conicalGradient = QConicalGradient(200,200,30)
      conicalGradient.setColorAt(0.0,Qt.white)
      conicalGradient.setColorAt(0.2,color)
      conicalGradient.setColorAt(1.0,Qt.black)
      self.area.setBrush(conicalGradient)
    elif(style == Qt.TexturePattern):
      self.area.setBrush(QBrush(QPixmap("images/brick.png")))
    else:
      self.area.setBrush(QBrush(color,style))
    
  
class PaintArea(QWidget):
  def __init__(self):
    super(PaintArea,self).__init__()
    self.Shape = ["Line","Rectangle", 'Rounded Rectangle', "Ellipse", "Pie", 'Chord', 
  "Path","Polygon", "Polyline", "Arc", "Points", "Text", "Pixmap"]
    self.setPalette(QPalette(Qt.white))
    self.setAutoFillBackground(True)
    self.setMinimumSize(400,400)
    self.pen = QPen()
    self.brush = QBrush()    
  
  def setShape(self,s):
    self.shape = s
    self.update()
    
  def setPen(self,p):
    self.pen = p
    self.update()
  
  def setBrush(self,b):
    self.brush = b
    self.update()
  
  def paintEvent(self,QPaintEvent):
    p = QPainter(self)
    p.setPen(self.pen)
    p.setBrush(self.brush)
    
    rect = QRect(50,100,300,200) 
    points = [QPoint(150,100),QPoint(300,150),QPoint(350,250),QPoint(100,300)]
    startAngle = 30 * 16
    spanAngle = 120 * 16
    
    path = QPainterPath();
    path.addRect(150,150,100,100)
    path.moveTo(100,100)
    path.cubicTo(300,100,200,200,300,300)
    path.cubicTo(100,300,200,200,100,100)
    
    if self.shape == "Line":
      p.drawLine(rect.topLeft(),rect.bottomRight())
    elif self.shape == "Rectangle":
      p.drawRect(rect)
    elif self.shape == 'Rounded Rectangle':
      p.drawRoundedRect(rect, 25, 25, Qt.RelativeSize)
    elif self.shape == "Ellipse":
      p.drawEllipse(rect)
    elif self.shape == "Polygon":
      p.drawPolygon(QPolygon(points),Qt.WindingFill)
    elif self.shape == "Polyline":
      p.drawPolyline(QPolygon(points))
    elif self.shape == "Points":
      p.drawPoints(QPolygon(points))
    elif self.shape == "Pie":
      p.drawPie(rect, startAngle, spanAngle)
    elif self.shape == "Arc":
      p.drawArc(rect,startAngle,spanAngle)
    elif self.shape == "Chord":
      p.drawChord(rect, startAngle, spanAngle)
    elif self.shape == "Path":
      p.drawPath(path)
    elif self.shape == "Text":
      p.drawText(rect,Qt.AlignCenter,"Hello Qt!")
    elif self.shape == "Pixmap":
      p.drawPixmap(150,150,QPixmap("images/qt-logo.png"))
    
if __name__=='__main__':
  app = QApplication(sys.argv)
  form = StockDialog()
  form.show()
  app.exec_()

以上这篇PyQt5利用QPainter绘制各种图形的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python使用reportlab画图示例(含中文汉字)
Dec 03 Python
Python中的模块和包概念介绍
Apr 13 Python
详解Python的Django框架中的通用视图
May 04 Python
Python中使用支持向量机(SVM)算法
Dec 26 Python
Python实现动态图解析、合成与倒放
Jan 18 Python
Python3安装Pillow与PIL的方法
Apr 03 Python
解决运行出现'dict' object has no attribute 'has_key'问题
Jul 15 Python
python读写数据读写csv文件(pandas用法)
Dec 14 Python
Python关于OS文件目录处理的实例分享
May 23 Python
pandas中DataFrame数据合并连接(merge、join、concat)
May 30 Python
关于Python使用turtle库画任意图的问题
Apr 01 Python
Elasticsearch 聚合查询和排序
Apr 19 Python
python连接数据库的方法
Oct 19 #Python
Python3使用PyQt5制作简单的画板/手写板实例
Oct 19 #Python
python里使用正则的findall函数的实例详解
Oct 19 #Python
详解python里使用正则表达式的全匹配功能
Oct 19 #Python
python中logging库的使用总结
Oct 18 #Python
R vs. Python 数据分析中谁与争锋?
Oct 18 #Python
Ubuntu安装Jupyter Notebook教程
Oct 18 #Python
You might like
实战mysql导出中文乱码及phpmyadmin导入中文乱码的解决方法
2010/06/11 PHP
PHP中include()与require()的区别说明
2017/02/14 PHP
如何用PHP做到页面注册审核
2017/03/02 PHP
PHP substr()函数参数解释及用法讲解
2017/11/23 PHP
支持汉转拼和拼音分词的PHP中文工具类ChineseUtil
2018/02/23 PHP
PHP实现的多维数组去重操作示例
2018/07/21 PHP
Laravel validate error处理,ajax,json示例
2019/10/25 PHP
php封装的page分页类完整实例代码
2020/02/01 PHP
给Javascript数组插入一条记录的代码
2007/08/30 Javascript
Jquery在IE7下无法使用 $.ajax解决方法
2009/11/11 Javascript
Javascript下IE与Firefox下的差异兼容写法总结
2010/06/18 Javascript
JS文本获得焦点清除文本文字的示例代码
2014/01/13 Javascript
js判断一个字符串是否包含一个子串的方法
2015/01/26 Javascript
利用jQuery实现漂亮的圆形进度条倒计时插件
2015/09/30 Javascript
js+canvas简单绘制圆圈的方法
2016/01/28 Javascript
JQuery EasyUI的使用
2016/02/24 Javascript
AngularJS 遇到的小坑与技巧小结
2016/06/07 Javascript
Bootstrap实现弹性搜索框
2016/07/11 Javascript
vue2.0 使用element-ui里的upload组件实现图片预览效果方法
2018/09/04 Javascript
JavaScript:ES2019 的新特性(译)
2019/08/08 Javascript
python进阶教程之动态类型详解
2014/08/30 Python
Python中获取网页状态码的两个方法
2014/11/03 Python
python实现统计代码行数的方法
2015/05/22 Python
Python函数可变参数定义及其参数传递方式实例详解
2015/05/25 Python
使用Python写CUDA程序的方法
2017/03/27 Python
Python中typing模块与类型注解的使用方法
2019/08/05 Python
pygame实现贪吃蛇游戏(下)
2019/10/29 Python
python pip安装包出现:Failed building wheel for xxx错误的解决
2019/12/25 Python
django自带的权限管理Permission用法说明
2020/05/13 Python
CSS3 函数技巧 用css 实现js实现的事情(clac Counters Tooltip)
2017/08/15 HTML / CSS
HTML5 Canvas如何实现纹理填充与描边(Fill And Stroke)
2013/07/15 HTML / CSS
amazeui树节点自动展开折叠面板并选中第一个树节点的实现
2020/08/24 HTML / CSS
印尼在线精品店:Berrybenka.com
2016/10/22 全球购物
Perfumetrader荷兰:香水、化妆品和护肤品在线商店
2017/09/15 全球购物
瑞典网上购买现代和复古家具:Reforma
2019/10/21 全球购物
2015出纳试用期工作总结
2014/12/12 职场文书