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

PyQt4学习笔记8之绘图

2015-12-19 17:58 176 查看

PyQt4中的绘图

单机此处查看原文。

当想要改变或者加强现有控件,或者创建自定义的控件时,在图形应用程序中,绘图操作是必须的。在PyQt4工具包中提供了用于绘图的API。

绘图操作在paintEvent()中完成。绘图代码应该置于QtGui.QPainter对象的begin()和end()方法之间。它在控件或其他绘图设备上执行较低级的图形绘制。

文本绘图

首先,以一个绘制Unicode文本的例子作为演示。

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

"""
ZetCode PyQt4 tutorial

In this example, we draw text in Russian azbuka.

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

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

def __init__(self):
super(Example, self).__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 = QtGui.QPainter()
qp.begin(self)
self.drawText(event, qp)
qp.end()

def drawText(self, event, qp):

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

def main():

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

if __name__ == '__main__':
main()


在上述示例中,我们使用Azbuka(西里尔字母)绘制文本。文本采用垂直和水平对齐方式。

def paintEvent(self, event):
...


绘图过程在绘图事件中完成。

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


QtGui.QPainter类负责处理所有的低级绘图操作。所有的绘图方法都应置于begin()和end()之间。实际的绘图操作则委托给drawText()方法。

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


上面的代码则指定了绘制文本所使用的画笔和字体。

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


drawText()方法在窗口上绘制文本。绘图事件的rect()方法返回需要被更新的矩形区域。



点绘制

一个点是绘图中最简单的图形对象。

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

"""
ZetCode PyQt4 tutorial

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

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

import sys, random
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

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

self.initUI()

def initUI(self):

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

def paintEvent(self, e):

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

def drawPoints(self, qp):

qp.setPen(QtCore.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)

def main():

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

if __name__ == '__main__':
main()


在上述示例程序中,我们随机地在窗口的客户区中画了1000个红色的点。

qp.setPen(QtCore.Qt.red)


上述代码使用预先定义的QtCore.Qt.red常值将画笔设置为红色。

size = self.size()


我们通过改变窗口大小来产生一个绘图事件,同时,使用size()方法获取当前窗口的大小。我们使用窗口的大小来分配窗口客户区点的分布。

qp.drawPoint(x, y)


最后,我们使用drawPoint()方法绘制点。



颜色

颜色是一种由红、绿、蓝(RGB)组合的强度值代表的对象。RGB值得取值范围为0~255。我们可以使用多种方式定义颜色。RGB值或十六进制值是最常用的方式。我们也可以使用红、绿、蓝以及阿尔法(Alpha)代表的RGBA值,这里我们添加了额外的信息表征透明度。当阿尔法的值为255时,表示完全不透明,0则表示完全透明,此时的颜色是不可见的。

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

"""
ZetCode PyQt4 tutorial

This example draws three rectangles in three
different colours.

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

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

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

self.initUI()

def initUI(self):

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

def paintEvent(self, e):

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

def drawRectangles(self, qp):

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

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

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

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

def main():

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

if __name__ == '__main__':
main()


在上述示例程序中,我们绘制了三个着色的区域。

color = QtGui.QColor(0, 0, 0)
color.setNameColor('#d4d4d4')


上面的代码中,我们使用十六进制形式定义了一个颜色值。

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


上面的代码则定义了一个画刷并利用画刷绘制矩形。画刷是用于绘制图形背景的基本图形对象。drawRect()方法接收四个参数,参数一和参数二分别代表x和y轴的值,参数三和参数四分别代表矩形的宽度和高度。该方法使用当前的画笔和画刷绘制矩形。



画笔

画笔(QtGui.QPen)是基本的图形对象。用于绘制线条、曲线和矩形、椭圆、多边形或其他形状的轮廓。

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

"""
ZetCode PyQt4 tutorial

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

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

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

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

self.initUI()

def initUI(self):

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

def paintEvent(self, e):

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

def drawLines(self, qp):

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

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

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

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

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

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

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

def main():

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

if __name__ == '__main__':
main()


在示例中,我们绘制了六条直线,它们分别使用了六种不同的画笔风格。其中,有五种是预定义的画笔风格。我们创建了一个自定义的画笔,最后一条直线使用自定义画笔绘制而成。

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


首先,创建一个QtGui.QPen的画笔对象,颜色为黑色,宽度为2个像素,这样我们能清楚的看到不同风格的画笔之间的不同之处。QtCore.Qt.SolidLine是预定义的画笔风格中的一种。

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


上面的代码中,我们创建了一个自定义的画笔,我们将画笔的风格设置为QtCore.Qt.CustomDashLine并调用setDashPattern()函数。列表中的数字定义了画笔的风格。必须是偶数个数字。奇数位上的数字代表破折号,偶数位上的数字代表空格。数字越大,空格或破折号就越大。我们采用1像素的破折号、4像素空格、5像素破折号、4像素空格的风。



画刷

画刷(QtGui.QBrush)同样也是基本的图形对象。它用于绘制图形对象的背景,例如矩形、椭圆、多边形。画刷有三种不同的风格:预定义画刷、倾斜(gradient)和纹理(texture)画刷。

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

"""
ZetCode PyQt4 tutorial

This example draws 9 rectangles in different
brush styles.

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

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

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

self.initUI()

def initUI(self):

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

def paintEvent(self, e):

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

def drawBrushes(self, qp):

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

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

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

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

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

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

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

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

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

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

def main():

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

if __name__ == '__main__':
main()


上述示例中,我们绘制了九中不同的矩形。

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


我们定义了一个画刷对象。我们将他赋值给painter对象,并通过调用drawRect()方法绘制矩形。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pyqt 应用 图形 控件