PyQt5視窗繪圖控制元件

比卡丘不皮發表於2020-12-21

這裡就簡單的介紹 QPainter、QPen、QBrush,這三個類的繪圖功能。

QPainter

 QPainter在QWidget上繪圖的時候,提供了高度優化的函式,可以使得QPainter可以繪製複雜的餅圖。

 設定畫筆的風格:

 程式碼:

# -*- coding: utf-8 -*-
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

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_())

 

# -*- coding: utf-8 -*-
 


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)
	demo  = Drawing()
	demo.show()
	sys.exit(app.exec_())

QPen的使用

# -*- coding: utf-8 -*-

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的使用

# -*- coding: utf-8 -*-


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的使用

# -*- coding: utf-8 -*-


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_())

本程式碼為《PyQt5快速開發與實戰》中的程式碼,本人執行一下,提供一些筆記記錄。喜歡我的部落格可以關注我的部落格。

相關文章