QPen
QPen(钢笔)是一个基本的图形对象,用于绘制直线,曲线或者给轮廓画出矩形,椭圆形,多边形及其他形状
实例:QPen的使用
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
class Drawing(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
#设置初始位置及窗口大小和标题
self.setGeometry(300,300,280,270)
self.setWindowTitle('钢笔样式例子')
def paintEvent(self,e):
qp=QPainter()
qp.begin(self)
#自定义的方法
self.drawLines(qp)
qp.end()
def drawLines(self,qp):
#颜色黑色,宽度2,线条样式:SolidLine,从坐标1绘制到坐标2,下面类似,不在标注
pen=QPen(Qt.black,2,Qt.SolidLine)
qp.setPen(pen)
qp.drawLine(20,40,520,40)
pen.setStyle(Qt.DashLine)
qp.setPen(pen)
qp.drawLine(20,80,250,80)
pen.setStyle(Qt.DashDotLine)
qp.setPen(pen)
qp.drawLine(20, 120, 250, 120)
pen.setStyle(Qt.DotLine)
qp.setPen(pen)
qp.drawLine(20, 160, 250, 160)
pen.setStyle(Qt.DashDotDotLine)
qp.setPen(pen)
qp.drawLine(20, 200, 250, 200)
pen.setStyle(Qt.CustomDashLine)
pen.setDashPattern([1,4,5,4])
qp.setPen(pen)
qp.drawLine(20, 240, 250, 240)
if __name__ == '__main__':
app=QApplication(sys.argv)
demo=Drawing()
demo.show()
sys.exit(app.exec_())
效果截图:
QBrush
QBrush(画刷)是一个基本的图形对象,用于填充如矩形,椭圆形,多边形等形状,QBrush有三种类型,预定义,过渡,和纹理图案
实例:QBrush的使用
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_())
效果截图: