''''
import sys
from PyQt5.QtGui import QPen,QBrush,QPainter
from PyQt5.QtWidgets import QApplication,QWidget
from PyQt5.QtCore import Qt
class DrawDemo(QWidget):
def __init__(self):
super(DrawDemo, self).__init__()
#self.setGeometry(400,400,300,300)
self.setWindowTitle('Qpen&QBrush')
def paintEvent(self,e):
qp=QPainter() #实例化一个画板
qp.begin(self) #开始
self.drawLines(qp) #自定义的方法
qp.end() #结束
def drawLines(self,qp):
#设置钢笔属性:颜色黑色,宽度2,线条样式:SolidLine,
pen=QPen(Qt.black,2,Qt.SolidLine)
qp.setPen(pen) #将钢笔的属性赋值给 画板的钢笔
qp.drawLine(20,40,520,40)
pen.setStyle(Qt.DashLine)
qp.setPen(pen)
qp.drawRect(100,80,250,180)
pen.setStyle(Qt.CustomDashLine)
pen.setDashPattern([1,4,5,4])
qp.setPen(pen)
qp.drawLine(20, 300, 500, 300)
if __name__ == '__main__':
app=QApplication(sys.argv)
demo=DrawDemo()
demo.show()
sys.exit(app.exec_())
'''
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Drawing(QWidget):
def __init__(self,parent=None):
super(Drawing, self).__init__(parent)
self.initUI()
def initUI(self):
self.setGeometry(300,300,365,280)
self.setWindowTitle('画刷例子')
self.show()
def paintEvent(self,e):
qp=QPainter()
qp.begin(self)
self.drawLines(qp)
qp.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.Dense3Pattern)
qp.setBrush(brush)
qp.drawRect(10, 105, 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 = Drawing()
demo.show()
sys.exit(app.exec_())
参考:PyQt5基本控件详解之QPen与QBrush(十八)_jia666666的博客-CSDN博客