PySide学习笔记第十二章-绘制

来源:互联网 发布:币交易平台 源码 编辑:程序博客网 时间:2024/05/17 20:21

当我们想改变或者优化一些空间的现实效果时,我们可以调用PySide的工具箱的API接口来实现

绘制操作包含在paintEvent()函数中,绘制的代码包含在QtGui.QPainter 对象的begin()以及end()函数中间

1、绘制文本 Drawing text --将Unicode文本显示在窗口客户端区域


# -*- coding: utf-8 -*-"""ZetCode PySide tutorialIn this example, we draw text in Russian azbuka.author: Jan Bodnarwebsite: zetcode.comlast edited: August 2011"""import sysfrom PySide import QtGui, QtCoreclass 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) # 绘制函数定义在beginend函数之间        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()
二、画点

# -*- coding: utf-8 -*-"""ZetCode PySide tutorialIn the example, we draw randomly 1000 red pointson the window.author: Jan Bodnarwebsite: zetcode.comlast edited: August 2011"""import sys, randomfrom PySide import QtGui, QtCoreclass 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()
三、颜色对象 --代表RGB值,最常见的是RGB10或16进制值,也可以用RGBA值,多了一个透明度通道,RGBA都在0-255之间,0代表全透明,255代表不透明

# -*- coding: utf-8 -*-"""ZetCode PySide tutorialThis example draws three rectangles in threedifferent colors.author: Jan Bodnarwebsite: zetcode.comlast edited: August 2011"""import sysfrom PySide import QtGui, QtCoreclass Example(QtGui.QWidget):    def __init__(self):        super(Example, self).__init__()        self.initUI()    def initUI(self):        self.setGeometry(300, 300, 350, 100)        self.setWindowTitle('Colors')        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()
四、笔对象QtGui.QPen 可以用来画直线、曲线、矩形,椭圆,多边形 等等

本示例画各种样式的线

# -*- coding: utf-8 -*-"""ZetCode PySide tutorialThis example draws three rectangles in threedifferent colors.author: Jan Bodnarwebsite: zetcode.comlast edited: August 2011"""import sysfrom PySide import QtGui, QtCoreclass 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()

五、画刷--有三种类型的画刷 (预定义型,梯度型,纹理型)

在本示例中,用9中不同的画刷类型画出9种不同的矩形块


0 0
原创粉丝点击