您的位置:首页 > 编程语言 > Qt开发

PyQt5教程(九)——绘图

2015-12-23 00:00 423 查看
摘要: python3+pyqt5的教程译文,源网址http://zetcode.com/gui/pyqt5/。未完待续

原文:http://zetcode.com/gui/pyqt5/painting/

PyQt5的绘图系统可用于渲染矢量图、图像和文本。如果想改变或增强已有的控件,或者想从头创建一个自定义控件时,我们就需要在程序中进行图形的绘制。我们可以使用PyQt5提供的绘图API进行绘图操作。

绘图要在
paintEvent()
方法中实现。在
QPainter
对象的
begin()
end()
方法间编写绘图代码。它会在控件或其他图形设备上进行低级的图形绘制。

绘制文本

我们先以窗体内Unicode文本的绘制为例。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial

In this example, we draw text in Russian azbuka.

author: Jan Bodnar
website: zetcode.com
last edited: September 2015
"""

import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QColor, QFont
from PyQt5.QtCore import Qt

class Example(QWidget):

def __init__(self):
super().__init__()

self.initUI()

def initUI(self):

self.text = u'\u041b\u0435\u0432 \u041d\u0438\u043a\u043e\u043b\u0430\
\u0435\u0432\u0438\u0447 \u0422\u043e\u043b\u0441\u0442\u043e\u0439: \n\
\u0410\u043d\u043d\u0430 \u041a\u0430\u0440\u0435\u043d\u0438\u043d\u0430'

self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('Draw text')
self.show()

def paintEvent(self, event):

qp = QPainter()
qp.begin(self)
self.drawText(event, qp)
qp.end()

def drawText(self, event, qp):

qp.setPen(QColor(168, 34, 3))
qp.setFont(QFont('Decorative', 10))
qp.drawText(event.rect(), Qt.AlignCenter, self.text)

if __name__ == '__main__':

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

示例中我们绘制了一些西里尔字母,文本是垂直且水平对齐的。

def paintEvent(self, event):
...

绘制工作在paintEvent的方法内部完成。

qp = QPainter()
qp.begin(self)
self.drawText(event, qp)
qp.end()

QPainter
负责所有的低级绘制工作,在它的begin()与end()间放置了绘图代码。实际的绘制工作由
drawText()
方法完成。

qp.setPen(QColor(168, 34, 3))
qp.setFont(QFont('Decorative', 10))

这里我们定义了用于绘制文本的画笔与字体对象。

qp.drawText(event.rect(), Qt.AlignCenter, self.text)

drawText()
会在窗体上进行文本的绘制。通过paint event(绘图事件)的
rect()
方法得到当前窗体的可绘图区域。



绘制圆点

点是可以绘制的最简单的图形对象。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial

In the example, we draw randomly 1000 red points
on the window.

author: Jan Bodnar
website: zetcode.com
last edited: January 2015
"""

import sys, random
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt

class Example(QWidget):

def __init__(self):
super().__init__()

self.initUI()

def initUI(self):

self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('Points')
self.show()

def paintEvent(self, e):

qp = QPainter()
qp.begin(self)
self.drawPoints(qp)
qp.end()

def drawPoints(self, qp):

qp.setPen(Qt.red)
size = self.size()

for i in range(1000):
x = random.randint(1, size.width()-1)
y = random.randint(1, size.height()-1)
qp.drawPoint(x, y)

if __name__ == '__main__':

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

示例中我们随机画了1000个红点。

qp.setPen(Qt.red)

将画笔设为红色。我们使用了预定义的Qt.red常量

size = self.size()

每次调整窗体尺寸都会生成一个paint event。我们可以通过这个event的size()方法得到窗体当前的尺寸。我们将这些点分配到窗体的各个区域。

qp.drawPoint(x, y)

通过
drawPoint()
方法绘制圆点。



颜色

颜色是用于表示红绿蓝(RGB)各色值组合体的对象。合法的RGB值在0到255之间。颜色的定义有多种方式,通常用10进制或16进制的数值表示。我们也可以使用RGBA表示,它代表了红色、绿色、蓝色与Alpha通道值。也就是说我们附加了一个表示透明度的信息。Alpha值为255表示完全不透明,为0表示完全透明,也就是说颜色是不可见的。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial

This example draws three rectangles in three
#different colours.

author: Jan Bodnar
website: zetcode.com
last edited: January 2015
"""

import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QColor, QBrush

class Example(QWidget):

def __init__(self):
super().__init__()

self.initUI()

def initUI(self):

self.setGeometry(300, 300, 350, 100)
self.setWindowTitle('Colours')
self.show()

def paintEvent(self, e):

qp = QPainter()
qp.begin(self)
self.drawRectangles(qp)
qp.end()

def drawRectangles(self, qp):

col = QColor(0, 0, 0)
col.setNamedColor('#d4d4d4')
qp.setPen(col)

qp.setBrush(QColor(200, 0, 0))
qp.drawRect(10, 15, 90, 60)

qp.setBrush(QColor(255, 80, 0, 160))
qp.drawRect(130, 15, 90, 60)

qp.setBrush(QColor(25, 0, 90, 200))
qp.drawRect(250, 15, 90, 60)

if __name__ == '__main__':

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

示例中我们绘制了三个不同颜色的矩形。

color = QColor(0, 0, 0)
color.setNamedColor('#d4d4d4')

这里我们使用16进制值定义了一个颜色对象。

qp.setBrush(QColor(200, 0, 0))
qp.drawRect(10, 15, 90, 60)

我们为QPainter设置了一个笔刷(Bursh)对象并用它绘制了一个矩形。笔刷是用于绘制形状背景的基本图形对象。
drawRect()
方法接受四个参数,前两个是起点的x,y坐标,后两个是矩形的宽和高。这个方法使用当前的画笔与笔刷对象进行绘制。



QPen(画笔)

QPen是一个基本图形对象。它用于绘制直线,曲线以及矩形、椭圆、多边形或其他图形的轮廓。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial

In this example we draw 6 lines using
different pen styles.

author: Jan Bodnar
website: zetcode.com
last edited: January 2015
"""

import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtCore import Qt

class Example(QWidget):

def __init__(self):
super().__init__()

self.initUI()

def initUI(self):

self.setGeometry(300, 300, 280, 270)
self.setWindowTitle('Pen styles')
self.show()

def paintEvent(self, e):

qp = QPainter()
qp.begin(self)
self.drawLines(qp)
qp.end()

def drawLines(self, qp):

pen = QPen(Qt.black, 2, Qt.SolidLine)

qp.setPen(pen)
qp.drawLine(20, 40, 250, 40)

pen.setStyle(Qt.DashLine)
qp.setPen(pen)
qp.drawLine(20, 80, 250, 80)

pen.setStyle(Qt.DashDotLine)
qp.setPen(pen)
qp.drawLine(20, 120, 250, 120)

pen.setStyle(Qt.DotLine)
qp.setPen(pen)
qp.drawLine(20, 160, 250, 160)

pen.setStyle(Qt.DashDotDotLine)
qp.setPen(pen)
qp.drawLine(20, 200, 250, 200)

pen.setStyle(Qt.CustomDashLine)
pen.setDashPattern([1, 4, 5, 4])
qp.setPen(pen)
qp.drawLine(20, 240, 250, 240)

if __name__ == '__main__':

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

示例中我们绘制了6条直线。每条直线使用了不同的画笔风格,其中有5个是PyQt5中预定义的,另外我们也自已实现了一个。最后那条线就是用我们自定义的画笔风格所画。

pen = QPen(Qt.black, 2, Qt.SolidLine)

我们创建了一个黑颜色的画笔对象,其宽度为2像素,这样可以看出画笔风格的不同之处。
Qt.SolidLine
是预定义的一种画笔风格。

pen.setStyle(Qt.CustomDashLine)
pen.setDashPattern([1, 4, 5, 4])
qp.setPen(pen)

这里我们定义了一个画笔风格。我们设置了Qt.CustomDashLine并调用了
setDashPattern()
方法,它的参数(一个数字列表)定义了一种风格,必须有偶数个数字;其中奇数表示绘制实线,偶数表示留空。数值越大,直线或空白就越大。这里我们定义了1像素的实线,4像素的空白,5像素实线,4像素空白。。。



QBrush(笔刷)

QBrush是一个基本图形对象。它用于绘制矩形、椭圆或多边形等图形的背景。笔刷有三种类型:预定义笔刷、渐变笔刷或纹理图案笔刷。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial

This example draws 9 rectangles in different
brush styles.

author: Jan Bodnar
website: zetcode.com
last edited: January 2015
"""

import sys
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtGui import QPainter, QBrush
from PyQt5.QtCore import Qt

class Example(QWidget):

def __init__(self):
super().__init__()

self.initUI()

def initUI(self):

self.setGeometry(300, 300, 355, 280)
self.setWindowTitle('Brushes')
self.show()

def paintEvent(self, e):

qp = QPainter()
qp.begin(self)
self.drawBrushes(qp)
qp.end()

def drawBrushes(self, qp):

brush = QBrush(Qt.SolidPattern)
qp.setBrush(brush)
qp.drawRect(10, 15, 90, 60)

brush.setStyle(Qt.Dense1Pattern)
qp.setBrush(brush)
qp.drawRect(130, 15, 90, 60)

brush.setStyle(Qt.Dense2Pattern)
qp.setBrush(brush)
qp.drawRect(250, 15, 90, 60)

brush.setStyle(Qt.Dense3Pattern)
qp.setBrush(brush)
qp.drawRect(10, 105, 90, 60)

brush.setStyle(Qt.DiagCrossPattern)
qp.setBrush(brush)
qp.drawRect(10, 105, 90, 60)

brush.setStyle(Qt.Dense5Pattern)
qp.setBrush(brush)
qp.drawRect(130, 105, 90, 60)

brush.setStyle(Qt.Dense6Pattern)
qp.setBrush(brush)
qp.drawRect(250, 105, 90, 60)

brush.setStyle(Qt.HorPattern)
qp.setBrush(brush)
qp.drawRect(10, 195, 90, 60)

brush.setStyle(Qt.VerPattern)
qp.setBrush(brush)
qp.drawRect(130, 195, 90, 60)

brush.setStyle(Qt.BDiagPattern)
qp.setBrush(brush)
qp.drawRect(250, 195, 90, 60)

if __name__ == '__main__':

app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())

在示例中我们绘制了9个不同的矩形。

brush = QBrush(Qt.SolidPattern)
qp.setBrush(brush)
qp.drawRect(10, 15, 90, 60)

我们定义了一个笔刷对象,然后将它设置给QPainter对象,并调用painter的
drawRect()
方法绘制矩形。



在这部分教程中我们学习了一些基本的图形绘制。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息