实战PyQt5: 116-画刷类QBrush
文章插图
QBrush简介QBrush类定义由QPainter绘制的形状的填充图案 。 一个画刷可以有颜色 , 填充图案 , 渐变和纹理属性 。
【实战PyQt5: 116-画刷类QBrush】样式填充使用Qt.BrushStyle枚举变量定义填充图案 。 默认的填充样式为Qt.NoBrush , 在这种样式下表示不填充所画的物体 。 标准填充样式为Qt.SolidPattern 。 可以在创建画刷对象是时候设置填充模式 , 也可以在创建对象后使用setSyle()函数来设置或者更改填充样式 。 下图显示了Qt中定义的填充模式
文章插图
QBrush各种填充样式(图片来源:doc.qt.io)
color()定义填充图案的颜色 , 该颜色可以是Qt.GlobalColor中预定义的演示 , 也可以是其他任何QColor颜色 。 gradient()定义所使用的渐变填充样式 ,Qt提供了三种不同的渐变填充QLinearGradient , QConicalGradient和QRadialGradient 。 当填充模式使用Qt.TexturePattern时 , 则可以在创建画刷时提供像素图或者使用setTexture()来创建具有纹理填充功能的画刷 。
注意:无论先前设置了什么样的填充模式 , 如果使用了setTexture() , 都会使填充模式变成Qt.TexturePattern 。 如果填充模式我渐变 , 则setColor函数不起作用 。
QBrush定义常用函数:
- setColor():更改当前设置的颜色 。
- color(): 获得当前设置的颜色 。
- gradient():获得画刷的渐变设置 。
- setColor(): 将画刷的颜色设置为指定的颜色 。
- color(): 获得画刷的颜色 。
- setStyle(): 设置画刷的填充样式 。
- style(): 获得画刷的填充样式 。
- setTexture(): 设置画刷的纹理填充图像 , 同时将画刷的填充样式设置为Qt.TexturePattern 。
- texture(): 返回纹理图像 。
- setTransform(): 设置画刷的变换矩阵 。 画刷的变换矩阵与QPainter的变换矩阵合并产生最终的绘制效果 。
- tansform(): 返回画刷的当前变换矩阵 。
import sys, mathfrom PyQt5 import QtCore, QtGui, QtWidgetsfrom PyQt5.QtCore import Qt, QPoint, QPointFfrom PyQt5.QtGui import (QColor,QPen, QPainter, QPainterPath, QPolygonF, QBrush,QLinearGradient, QConicalGradient, QRadialGradient, QGradient,QPixmap)from PyQt5.QtWidgets import (QApplication, QWidget, QHBoxLayout, QFormLayout,QLabel, QFrame, QSizePolicy, QSpinBox, QPushButton,QColorDialog, QComboBox, QFileDialog) class MyFrame(QFrame):def __init__(self, parent = None):super(MyFrame, self).__init__(parent)self.setFrameShape(QFrame.Box)self.setFrameShadow(QFrame.Plain)self.setLineWidth(1)self.setMidLineWidth(0)self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)self.brushColor = Qt.blackself.pattern = Qt.SolidPatternself.filename = ''def setBrushColor(self, color):self.brushColor = colorself.update()def setPattern(self, pattern):self.pattern = patternself.update()def setTexture(self, filename):self.pattern = Qt.TexturePatternself.filename = filenameself.update()def paintEvent(self, event):painter = QPainter(self)painter.setRenderHint(QPainter.Antialiasing, True)#绘制边框线painter.drawRect(self.rect())pen = QPen()pen.setColor(Qt.darkBlue)painter.setPen(pen)brush = QBrush()brush.setColor(self.brushColor)if self.pattern == Qt.TexturePattern:brush.setTexture(QPixmap(self.filename))elif self.pattern == Qt.LinearGradientPattern:lg = QLinearGradient(0, 0, 100, 0)lg.setColorAt(0, Qt.red)#lg.setSpread(QGradient.RepeatSpread)lg.setColorAt(1, Qt.blue)brush = QBrush(lg)elif self.pattern == Qt.RadialGradientPattern:rg = QRadialGradient(50, 50, 200, 100, 100)rg.setColorAt(0, Qt.red)rg.setColorAt(1, Qt.blue)rg.setSpread(QGradient.RepeatSpread)brush = QBrush(rg)elif self.pattern == Qt.ConicalGradientPattern:cg = QConicalGradient(100, 100, 120)cg.setColorAt(0, Qt.red)cg.setColorAt(1, Qt.blue)cg.setSpread(QGradient.RepeatSpread)brush = QBrush(cg)else:brush.setStyle(self.pattern)painter.setBrush(brush)painter.drawRect(10, 10, 200, 160)painter.drawEllipse(300, 10, 160, 160)painter.drawPie(10, 240, 240, 240, 30 * 16, 110 * 16)#绘制五角星#添加一个多边形(五角星)#外点:x=Rcos(72°*k)y=Rsin(72°*k)k=0,1,2,3,4#内点:r=Rsin18°/sin36°x=rcos(72°*k+36°)y=rsin(72°*k+36°)k=0,1,2,3,4deg_18 = 18 * math.pi / 180deg_36 = 36 * math.pi / 180deg_72 = 72 * math.pi / 180r_out = 100 #半径r_inner = r_out * math.sin(deg_18) / math.sin(deg_36)polygon = QPolygonF()for i in range (5) :#外点out_angle = deg_72 * i - deg_18polygon.append(QPointF(r_out * math.cos(out_angle), r_out * math.sin(out_angle)))#内点in_angle =deg_72 * i + deg_18polygon.append(QPointF(r_inner * math.cos(in_angle), r_inner * math.sin(in_angle)))painter.translate(380, 280)painter.drawPolygon(polygon, Qt.WindingFill)class DemoBrush(QWidget):def __init__(self, parent=None):super(DemoBrush, self).__init__(parent)# 设置窗口标题self.setWindowTitle('实战PyQt5: QBrush演示')#设置尺寸self.resize(720, 400)self.initUi()def initUi(self):layout = QHBoxLayout()self.canvas = MyFrame()#颜色设置btnSetColor = QPushButton('设置颜色')btnSetColor.clicked.connect(self.onSetBrushColor)#填充模式cmbBrusStyle = QComboBox()cmbBrusStyle.addItems(['NoBrush', 'SolidPattern', 'Dense1Pattern', 'Dense2Pattern','Dense3Pattern', 'Dense4Pattern','Dense5Pattern','Dense6Pattern','Dense7Pattern', 'HorPattern', 'VerPattern', 'CrossPattern','BDiagPattern', 'FDiagPattern', 'DiagCrossPattern'])cmbBrusStyle.currentTextChanged.connect(self.onBrushStyleChanged)cmbBrusStyle.setCurrentIndex(1)#渐变cmbGradient = QComboBox()cmbGradient.addItems(['None', 'LinearGradientPattern', 'RadialGradientPattern', 'ConicalGradientPattern'])cmbGradient.currentTextChanged.connect(self.onGradientChanged)#纹理btnTexture = QPushButton('打开图像文件')btnTexture.clicked.connect(self.onButtonTextureClicked)fLayout = QFormLayout()fLayout.setContentsMargins(0,0,0,0)fLayout.addRow('颜色: ', btnSetColor)fLayout.addRow('填充模式: ', cmbBrusStyle)fLayout.addRow('渐变: ', cmbGradient)fLayout.addRow('纹理: ', btnTexture)wid_left = QWidget()wid_left.setMaximumWidth(200)wid_left.setLayout(fLayout)layout.addWidget(wid_left)layout.addWidget(self.canvas)self.setLayout(layout)def onSetBrushColor(self):color = QColorDialog.getColor()self.canvas.setBrushColor(color)def onBrushStyleChanged(self, style):if style == 'SolidPattern':self.canvas.setPattern(Qt.SolidPattern)elif style == 'Dense1Pattern':self.canvas.setPattern(Qt.Dense1Pattern)elif style == 'Dense2Pattern':self.canvas.setPattern(Qt.Dense2Pattern)elif style == 'Dense3Pattern':self.canvas.setPattern(Qt.Dense3Pattern)elif style == 'Dense4Pattern':self.canvas.setPattern(Qt.Dense4Pattern)elif style == 'Dense5Pattern':self.canvas.setPattern(Qt.Dense5Pattern)elif style == 'Dense6Pattern':self.canvas.setPattern(Qt.Dense6Pattern)elif style == 'Dense7Pattern':self.canvas.setPattern(Qt.Dense7Pattern)elif style == 'HorPattern':self.canvas.setPattern(Qt.HorPattern)elif style == 'VerPattern':self.canvas.setPattern(Qt.VerPattern)elif style == 'CrossPattern':self.canvas.setPattern(Qt.CrossPattern)elif style == 'BDiagPattern':self.canvas.setPattern(Qt.BDiagPattern)elif style == 'FDiagPattern':self.canvas.setPattern(Qt.FDiagPattern)elif style == 'DiagCrossPattern':self.canvas.setPattern(Qt.DiagCrossPattern)else:self.canvas.setPattern(Qt.NoBrush)def onGradientChanged(self, style):if style == 'LinearGradientPattern':self.canvas.setPattern(Qt.LinearGradientPattern)elif style == 'RadialGradientPattern':self.canvas.setPattern(Qt.RadialGradientPattern)elif style == 'ConicalGradientPattern':self.canvas.setPattern(Qt.ConicalGradientPattern)else:self.canvas.setPattern(Qt.NoBrush)def onButtonTextureClicked(self):path,_ = QFileDialog.getOpenFileName(self, '打开图像文件', '', '图像文件 (*.png)')if path:self.canvas.setTexture(path)if __name__ == '__main__':app = QApplication(sys.argv)window = DemoBrush()window.show()sys.exit(app.exec())
- Wireshark数据包分析实战:TCP报文段重组
- Python爬虫采集网易云音乐热评实战
- Django实战016:django中使用redis详解
- HTTP实战之Wireshark抓包分析
- Wireshark数据包分析实战:网卡卸载
- Python数据分析:数据可视化实战教程
- 实战经验:电商平台遭遇CC攻击,我们是如何应对的?
- Tencent IN对话 | 八位互联网实战家,实战智慧营销商学院
- HLS实战之Wireshark抓包分析
- Vue实战091:Vue项目部署到nginx服务器