PyQt之窗口绘图类控件(QPainter、Qpen、QBrush)
在PyQt5中一般可以通过QPainter、QPen、QBrush这三个类来实现绘图功能,另外QPixmap也可以被看作为绘图类,因其图像的呈现方式在本质上来说也是通过绘图的方式将本地的图片进行呈现。
QPainter类在QWidget控件上执行绘图操作,他是一个绘图工具,为大部分图形界面提供了高度优化的函数,使QPainter类可以绘制从简单的直线到复杂的饼图等。
QPainter绘制操作实在QWIdget.painEvent()中完成的,且绘制方法必须在QtGui.QPainter对象的begin()和end()之间。
1.1QPainter类的常用方法
begin():
drawArc():
drawEllipse():
drawLine(int x1,int y1,int x2,int y2)
drawPixmap():
drawPolygon():
drawRect(int x,int y,int w,int h)
deawText():
fillRect():
setPen():
setBrush():
import sys
from PyQt5.QtWidgets import QApplication ,QWidget
from PyQt5.QtGui import QPainter ,QColor ,QFont
from PyQt5.QtCore import Qt
class Drawing(QWidget):
def __init__(self,parent=None):
super(Drawing,self).__init__(parent)
self.setWindowTitle("绘制文字")
self.resize(200, 200)
self.text = 'Hello World'
def paintEvent(self,event):
painter = QPainter(self)
painter.begin(self)
# 自定义的绘画方法
self.drawText(event, painter)
painter.end()
def drawText(self, event, qp):
# 设置笔的颜色
qp.setPen( QColor(3, 20, 160) )
# 设置字体
qp.setFont( QFont('SimSun', 20))
# 画出文本
qp.drawText(event.rect(), Qt.AlignCenter, self.text)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = Drawing()
win.show()
sys.exit(app.exec_())
import sys, math
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
class Drawing(QWidget):
def __init__(self, parent=None):
super(Drawing, self).__init__(parent)
self.resize(300, 200)
self.setWindowTitle("画点")
def paintEvent(self, event):
qp = QPainter()
qp.begin(self)
# 自定义画点方法
self.drawPoints(qp)
qp.end()
def drawPoints(self, qp):
qp.setPen( Qt.red)
size = self.size()
for i in range(1000):
# [-100, 100]两个周期的正弦函数图像
x = 100 *(-1+2.0*i/1000)+ size.width()/2.0
y = -50 * math.sin((x - size.width()/2.0)*math.pi/50) + size.height()/2.0
qp.drawPoint(x, y)
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Drawing()
win.show()
sys.exit(app.exec_())
QPen(钢笔)是一个基本的图形对象,用于绘制直线、曲线或者给出路包括画出矩形、椭圆形等其他形状。
例子:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
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):
pen=QPen(Qt.black,2,Qt.SolidLine)
qp.setPen(pen)
qp.drawLine(20,40,250,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,240,160)
pen.setStyle(Qt.DashDotDotLine)
qp.setPen(pen)
qp.drawLine(20,200,250,200)
#自定义线条样式
pen.setStyle(Qt.CustomDashLine)#创建线条样式
pen.setDashPattern([1,4,5,4])#定义线条样式
'''数字列表的个数必须是偶数,奇数位(1,3,5,7,9等)代表一端横线,
偶数位(2,4,6,8,10等)代表两端横线之间的空余距离,数字越大,
横线和控与激励就越大,本例中含义为,1像素宽度的横线,4像素宽度的空余距离,
5像素宽度的横线,4像素宽度的空余距离'''
qp.setPen(pen)
qp.drawLine(20,240,250,240)
if __name__=="__main__":
app=QApplication(sys.argv)
win=Drawing()
win.show()
sys.exit(app.exec_())
QBrush(画刷)是一个基本的图形对象,用于填充如矩形,椭圆形或者多边形等形状。
QBrush有三种类型:
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
class Drawing(QWidget):
def __init__(self):
super().__init__()
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)
win = Drawing()
win.show()
sys.exit(app.exec_())
QPixmap类用于绘图设备的图像显示,他可以作为一个QPaintDevice对象,也可以加载到一个控件中,通常是标签或按钮,用于在标签或按钮上显示图像。
QPixmap可以读取的图像文件类型主要有:BMP、GIF、JPG、JPEG、PNG、PBM等
QPixmap类中的常用方法有:
copy():
fromImage():
grabWidget():
grabWindow():
load():
save():
toImage():
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class QPixmapWin(QWidget):
def __init__(self,parent=None):
super(QPixmapWin,self).__init__(parent)
self.lab1=QLabel(self)
self.lab1.setPixmap(QPixmap("./images/python.jpg"))
layout=QVBoxLayout()
layout.addWidget(self.lab1)
self.setLayout(layout)
self.setWindowTitle("QPixmap例子")
if __name__=="__main__":
app=QApplication(sys.argv)
win=QPixmapWin()
win.show()
sys.exit(app.exec_())