从零开始学Pyqt5之【控件介绍】(15):绘图类控件QPainter、QPen、QBrush、QPixMap
QPainter控件
QPainter控件的作用
QPainter 类在 QWidget(控件)上执行绘图操作,它是一个绘制工具,为大部分图形界面提供了高度优化的函数,使QPainter类可以绘制从简单的直线到复杂的饼图等。
绘制操作在 **QWidget.paintEvent()**函数中完成。绘制方法必须放在 QtGui.QPainter对象的begin()和 end()之间。
QPainter控件常用方法
QPainter代码示例:1、在窗口中绘制文字
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(300, 200)
self.text = '欢迎学习 PyQt5'
def paintEvent(self,event):
painter = QPainter(self)
painter.begin(self)
self.drawText(event, painter)
painter.end()
def drawText(self, event, qp):
qp.setPen( QColor(168, 34, 3) )
qp.setFont( QFont('SimSun', 20))
qp.drawText(event.rect(), Qt.AlignCenter, self.text)
if __name__ == "__main__":
app = QApplication(sys.argv)
demo = Drawing()
demo.show()
sys.exit(app.exec_())
代码运行结果:
QPainter代码示例:2、绘制点
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):
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)
demo = Drawing()
demo.show()
sys.exit(app.exec_())
代码运行结果:
QPen控件
QPen控件的作用
QPen(钢笔)是一个基本的图形对象,用于绘制直线、曲线或者给轮廓画出矩形、椭圆形、多边形及其他形状等。 可以设置钢笔的风格,钢笔的风格如下:
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, 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有三种类型:预定义、过渡和纹理图案。
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)
demo = Drawing()
demo.show()
sys.exit(app.exec_())
代码运行结果:
QPixMap控件
QPixMap控件作用
QPixmap类用于绘图设备的图像显示,它可以作为一个 QPaintDevice对象,也可以加载到一个控件中,通常是标签或按钮,用于在标签或按钮上显示图像。 QPixmap可以读取的图像文件类型有 BMP、GIF、JPG、JPEG、PNG、PBM、PGM、PPM、 XBM、 XPM 等。
QPixMap常用方法
QPixMap代码示例
将图片显示在QLabel空间上
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
if __name__ == '__main__':
app = QApplication(sys.argv)
win = QWidget()
lab1 = QLabel()
lab1.setPixmap(QPixmap("./images/python.jpg"))
vbox=QVBoxLayout()
vbox.addWidget(lab1)
win.setLayout(vbox)
win.setWindowTitle("QPixmap 例子")
win.show()
sys.exit(app.exec_())
代码运行结果:
|