W e S D
0 1

读Pyqt4教程,带你入门Pyqt4 _012

颜色

颜色是指一个代表红(Red)、绿(Green)、蓝(Blue)(RGB)强度值组合的对象,有效的RGB值在0~255之间。我们可以用多种方式定义颜色,最常用的是RGB十进制或者十六进制值。也可以使用RGBA值,表示红(Red)、绿(Green)、蓝(Blue)和透明度(Alpha)。这里我们增加了额外的信息——关于透明度。Alpha值是255表明完全不透明,0是全透明,即颜色不可见。

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

# colors.py

import sys, random
from PyQt4 import QtGui, QtCore


class Example(QtGui.QWidget):

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

        self.setGeometry(300, 300, 350, 280)
        self.setWindowTitle('Colors')

    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(255, 0, 0, 80))
        qp.drawRect(10, 15, 90, 60)

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

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

        qp.setBrush(QtGui.QColor(10, 163, 2, 55))
        qp.drawRect(10, 105, 90, 60)

        qp.setBrush(QtGui.QColor(160, 100, 0, 255))
        qp.drawRect(130, 105, 90, 60)

        qp.setBrush(QtGui.QColor(60, 100, 60, 255))
        qp.drawRect(250, 105, 90, 60)

        qp.setBrush(QtGui.QColor(50, 50, 50, 255))
        qp.drawRect(10, 195, 90, 60)

        qp.setBrush(QtGui.QColor(50, 150, 50, 255))
        qp.drawRect(130, 195, 90, 60)

        qp.setBrush(QtGui.QColor(223, 135, 19, 255))
        qp.drawRect(250, 195, 90, 60)


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

在例子中,我们绘制了9个有色矩形,第一行显示红色,具有不同的透明度。

paint.setBrush(QtGui.QColor(255, 0, 0, 80));
paint.drawRect(10, 15, 90, 60)

这里我们定义一个画刷并绘制一个矩形,画刷从一个初级图形对象,用来绘制图形的背景。 drawRect() 方法接受四个参数。头两个是坐标轴的x和y,第三和第四个是矩形的宽高,该方法使用当前的画笔和画刷绘制一个矩形。

QPen

QPen 是初级图形对象,用来绘制线条、曲线和矩形、椭圆、多边形或其他形状的轮廓。

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

# penstyles.py

import sys
from PyQt4 import QtGui, QtCore


class Example(QtGui.QWidget):

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

        self.setGeometry(300, 300, 280, 270)
        self.setWindowTitle('penstyles')

    def paintEvent(self, e):

        qp = QtGui.QPainter()

        qp.begin(self)
        self.doDrawing(qp)
        qp.end()

    def doDrawing(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)


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

该例子中,我们绘制了6条线,使用了不同的画笔样式。其中5个预定义样式,我们也可以创建自定义样式,最后一个使用了自定义的样式。

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

这里我们自定义了一个画笔样式,设置 QtCore.Qt.CustomDashLine 样式,并调用 setDashPattern() 方法。用一列数字定义样式,必须是一个偶数序列,技术定义破折号,而是定义间隔,数字越大,间隔或破折号越大。我们的样式是1像素的破折号、3像素的间隔、5像素的破折号和4像素的间隔。

QBrush

QBrush 是初级图形对象,用来绘制图形的背景,如:矩形,椭圆或多边形。画刷有三种类型。可以是预定义的渐变或纹理图案。

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

# brushes.py

import sys
from PyQt4 import QtGui, QtCore


class Example(QtGui.QWidget):

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

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

    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)


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

该例子中,我们绘制了9个不同的矩形。

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

定义画刷对象,并设置到绘画者对象,调用 drawRect 方法绘制矩形。

在PyQt4教程的这部分中,我们做了一些基本的绘画。

 

 

 

本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 
转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4593922.html

 

 

posted @ 2015-06-22 21:24  SD.Team  阅读(360)  评论(0编辑  收藏  举报