QBrush(画刷)是一个基本的图形对象,用于填充如矩形、椭圆形或多边形等形状。QBrush有三种类型:预定义、过渡和纹理图案。
案例28 QBrush的使用
import sys import math from PyQt5.QtWidgets import QWidget, QApplication from PyQt5.QtGui import QPainter, QColor, QFont, QBrush from PyQt5.QtCore import Qt class DrawingDemo(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.setGeometry(300, 300, 365, 280) self.setWindowTitle("画刷例子") def paintEvent(self, event): painter = QPainter(self) painter.begin(self) self.drawLines(painter) painter.end() def drawLines(self, qp): brush = QBrush(Qt.SolidPattern) qp.setBrush(brush) qp.drawRect(10, 15, 90, 60) brush = QBrush(Qt.Dense1Pattern) qp.setBrush(brush) qp.drawRect(130, 15, 90, 60) brush = QBrush(Qt.Dense2Pattern) qp.setBrush(brush) qp.drawRect(250, 15, 90, 60) brush = QBrush(Qt.DiagCrossPattern) qp.setBrush(brush) qp.drawRect(10, 105, 90, 60) brush = QBrush(Qt.Dense5Pattern) qp.setBrush(brush) qp.drawRect(130, 105, 90, 60) brush = QBrush(Qt.Dense6Pattern) qp.setBrush(brush) qp.drawRect(250, 105, 90, 60) brush = QBrush(Qt.HorPattern) qp.setBrush(brush) qp.drawRect(10, 195, 90, 60) brush = QBrush(Qt.VerPattern) qp.setBrush(brush) qp.drawRect(130, 195, 90, 60) brush = QBrush(Qt.BDiagPattern) qp.setBrush(brush) qp.drawRect(250, 195, 90, 60) if __name__ == "__main__": app = QApplication(sys.argv) demo = DrawingDemo() demo.show() sys.exit(app.exec_())