PyQt5利用QPainter绘制各种图形

这个例子我做了好几天:

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

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

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

【知识点】

1、关于多个点的使用

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

请看:

 1      # 定义多个点
 2      points = [QPoint(10, 80), QPoint(20, 10), QPoint(80, 30), QPoint(90, 70)]
 3
 4      # ===直接使用 points 会报错!=========
 5      # ...
 6      elif self.shape == self.Points:
 7            painter.drawPoints(points)
 8
 9      elif self.shape == self.Polyline:
10            painter.drawPolyline(points)
11
12      elif self.shape == self.Polygon:
13            painter.drawPolygon(points, 4)
14
15      # ...
16
17      # ===把 points 用 QPolygon()包裹起来才正确!=========
18      # ...
19      elif self.shape == self.Points:
20            painter.drawPoints(QPolygon(points))
21
22      elif self.shape == self.Polyline:
23            painter.drawPolyline(QPolygon(points))
24
25      elif self.shape == self.Polygon:
26            painter.drawPolygon(QPolygon(points), 4)
27
28      # ...

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

【效果图】

【资源】

http://doc.qt.io/qt-5/images/used-in-examples/painting/basicdrawing/images/brick.png

http://doc.qt.io/qt-5/images/used-in-examples/painting/basicdrawing/images/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_()

(罗兵 作于2015.02.05     01:58)

时间: 2024-10-07 19:29:52

PyQt5利用QPainter绘制各种图形的相关文章

利用QPainter绘制各种图形(Shape, Pen 宽带,颜色,风格,Cap,Join,刷子)

利用QPainter绘制各种图形 Qt的二维图形引擎是基于QPainter类的.QPainter既可以绘制几何形状(点.线.矩形.椭圆.弧形.弦形.饼状图.多边形和贝塞尔曲线),也可以绘制像素映射.图像和文字.此外,QPainter还支持一些高级特性,例如反走样(针对文字和图形边缘).像素混合.渐变填充和矢量路径等.QPainter也支持线性变换,例如平移.旋转.错切和缩放. 本例子中利用QPainter类提供的各种draw函数,绘制各种类型的图形,包括对图形的形状.颜色.填充风格等的选择. 1

利用QPainter绘制散列图

[1]实例代码 (1)代码目录结构(备注:QtCreator默认步骤新建工程) (2)工程pro文件 1 QT += core gui 2 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 5 TARGET = painter 6 TEMPLATE = app 7 8 9 SOURCES += main.cpp10 mainwindow.cpp 11 12 HEADERS += mainwindow.h 13 14 FORMS += main

利用canvas绘制图形

绘制图有很多种方法,可以借助flash实现,也可以使用SVG和VML来绘图.本章将要学习一种新的绘图方法--使用Canvas元素,它是基于HTML5原生的绘图功能.使用Canvas元素,可以绘制图形,也可以实现动画.它方便了使用Javascript脚本的前端开发人员,寥寥的竖行代码,就可以在Canvas元素中实现各种图形及动画.本章将介绍如何使用Canvas元素来绘制一些简单的图形.本章主要知识点如下:·认识Canvas元素·使用Canvas绘图·Canvas与JavaScript之间的互动·利

利用 turtle库绘制简单图形

turtle库是python的基础绘图库,这个库被介绍为一个最常用的用来介绍编程知识的方法库,其主要是用于程序设计入门,是标准库之一,利用turtle可以制作很多复杂的绘图. turtle名称含义为“海龟”,我们想象一只海龟,位于显示器上窗体的正中心,在画布上游走,它游走的轨迹就形成了绘制的图形.海龟的运动是由程序控制的,它可以变换颜色,改变大小(宽度)等. 绘图坐标体系 turtle.setup(width,height,startx,starty) 使用turtle的setup函数,可以在屏

Android利用canvas画各种图形

canvas通俗的说就是一张画布,我们可以使用画笔paint,在其上面画任意的图形. 原理: 可以把canvas视为Surface的替身或者接口,图形便是绘制在Surface上的.Canvas封装了所有的绘制调用.通过Canvas, 绘制到Surface上的内容首先存储到一个内存区域(也就是对应的Bitmapz中),该Bitmap最终会呈现到窗口上. 使用: 1.Canvas可以直接new Canvas(): 2.在View中重写OnDraw()方法,里面有一个Canvas,今天讨论的内容. 方

利用JFreeChart绘制股票K线图完整解决方案

http://blog.sina.com.cn/s/blog_4ad042e50100q7d9.html 利用JFreeChart绘制股票K线图完整解决方案 (2011-04-30 13:27:17) 标签: 绘制 股票 k线 it 分类: 软件_Software 因为工作的需要,接触了一些股票图形绘制类的工作,其中最主要的还是股票K线图的绘制了,如果利用编程语言最底层的图形绘制方法去绘制这类图形,如果对编程语言不是特别熟悉的话,一般是有很大的困难的,通过在网上搜索发现,以自己最熟悉的两门语言为

css绘制特殊图形,meida查询,display inline-box间隙问题以及calc()函数

本文同时发表于本人个人网站 www.yaoxiaowen.com 距离上一篇文章已经一个月了,相比于写代码,发现写文章的确是更需要坚持的事情.言归正传,梳理一下这一个月来,在写ife任务时,有必要记录的问题吧.一,css画特殊图形. 在网页中,需要一些特殊图形时.比如半圆形,三角形等,我们一般是让ui切图,但是实际上,利用css的边框 (border)属性,我们可以绘制一些有规律的图形. 盒模型中元素有四个角,那么每个角就存在一个弧度的属性. 先看代码: 1 .half_circle{ 2 wi

如何在ChemDraw中绘制透视图形

ChemDraw虽然是一款常用的化学图形绘制软件,但是很多的用户对其使用还是了解的不多,因此,我们希望通过做一些教程来给大家做个参照.本教程将如何用ChemDraw软件绘制霍沃思(Haworth)D-葡萄糖结构透视图.  D-葡萄糖结构透视图形 绘制实例之前先进行以下操作: (1)从文件菜单选择New document. (2)从文件菜单选择Save as,出现存储对话框. (3)在对话框的文件名框中,输入tul5.cdx. (4)选择存储格式,在其中存储文件. (5)单击OK按钮. 以下是实例

学习笔记:HTML5 Canvas绘制简单图形

HTML5 Canvas绘制简单图形 1.添加Canvas标签,添加id供js操作. <canvas id="mycanvas" height="700" width="1024" style="border:1px solid #aaa;text-align:center;"> 你的浏览器不支持Canvas,请更新浏览器再试!!! </canvas> 在canvas标签之间应该做浏览器是否支持的检测,