当前位置:网站首页>Pyqt5 rapid development and practice 4.10 window drawing controls
Pyqt5 rapid development and practice 4.10 window drawing controls
2022-07-27 22:56:00 【Ding Jiaxiong】
PyQt5 Rapid development and actual combat
List of articles
4. The first 4 Chapter PyQt5 Basic window control
4.10 Window drawing control
stay PyQt5 in , In general, you can go through QPainter,QPen and QBrush These three classes are used to realize the drawing function . Besides ,QPixmap The function of is to load and render local images , In essence, the presentation of images is also realized by drawing , therefore QPixmap It can also be regarded as a class of drawing .
4.10.1 QPainter
QPainter Class in QWidget( Control ) Perform drawing operations on , It is a drawing tool , It provides highly optimized functions for most graphical interfaces , send QPainter Class can draw from simple lines to complex pie charts .
The drawing operation is in QWidget.paintEvent() Finish in . The drawing method must be placed in QtGui.QPainter Object's begin() and end() Between .QPainter Class performs lower level graphics rendering functions on controls or other drawing devices .
Related methods :
| Method | describe |
|---|---|
| begin() | Start drawing on the target device |
| drawArc() | Draw an arc between the starting angle and the final angle |
| drawEllipse | Draw an ellipse inside a rectangle |
| drawLine(int x1, int y1, int x2, int y2) | Draw a line with endpoint coordinates specified . Draw from (xl, yl) To (x2, y2) And set the current brush position to (x2, y2) |
| drawPixmap() | Extract from image file Pixmap And display it in the specified position |
| drawPolygon() | Draw polygons using an array of coordinates |
| drawRect(int x,int y,int w,int h) | With a given width w And height h From the top left coordinates (x,y) Draw a rectangle |
| drawText() | Displays the text at the given coordinates |
| fillRect() | Use QColor Parameter fills the rectangle |
| setBrush() | Set brush style |
| setPen() | Set the color of the pen used for drawing 、 Size and style |
Brush style :
| Enumeration type | describe |
|---|---|
| Qt.NoPen | No line . such as QPainter.drawRect() fill , But no boundary line was drawn |
| Qt.SolidLine | A simple line |
| Qt.DashLine | Text messages separated by some pixels |
| Qt.DotLine | Dots separated by some pixels |
| Qt.DashDotLine | Alternating dots and short lines |
| Qt.DashDotDotLine | A short line 、 Two points |
| Qt.MPenStyle | Brush style mask |
Case study —— Draw text
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(" Draw text in the window ")
self.resize(300 ,200)
self.text = " It's windy every year , The wind blows every year "
def paintEvent(self,event):
painter = QPainter(self)
painter.begin(self)
# Custom drawing method
self.drawText(event,painter)
painter.end()
def drawText(self,event ,qp):
# Set the color of the brush
qp.setPen(QColor(168,34,3))
# Set the font
qp.setFont(QFont('SimSun',20))
# Draw text
qp.drawText(event.rect(),Qt.AlignCenter,self.text)
if __name__ == '__main__':
from pyqt5_plugins.examples.exampleqmlitem import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
win = Drawing()
win.show()
sys.exit(app.exec_())

Case study —— Draw points
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(" Draw points in the window ")
def paintEvent(self,event):
## Initialize the drawing tool
qp = QPainter()
# Start drawing in the window
qp.begin(self)
# Custom dot drawing method
self.drawPoint(qp)
# Finish drawing
qp.end()
def drawPoint(self,qp):
qp.setPen(Qt.red)
size = self.size()
for i in range(1000):
# Draw sine function image
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__':
from pyqt5_plugins.examples.exampleqmlitem import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
win = Drawing()
win.show()
sys.exit(app.exec_())

4.10.2 QPen
QPen( Pen ) Is a basic graphic object , Used to draw lines 、 Curve or draw a rectangle for the outline 、 ellipse 、 Polygons and other shapes .
Case study ——QPen Use
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(" Pen style case ")
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__':
from pyqt5_plugins.examples.exampleqmlitem import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
win = Drawing()
win.show()
sys.exit(app.exec_())

4.10.3 QBrush
QBrush( A brush ) Is a basic graphic object , Used to fill a rectangle 、 Oval or polygonal shape .QBrush There are three types : predefined 、 Transition and texture patterns .
Case study ——QBrush Use
# -*- coding: utf-8 -*-
""" 【 brief introduction 】 In drawing QBrush Example , Draw nine rectangles of different styles . """
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(' Brush case ')
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__':
from pyqt5_plugins.examples.exampleqmlitem import QtCore
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
app = QApplication(sys.argv)
win = Drawing()
win.show()
sys.exit(app.exec_())

4.10.4 QPixmap
QPixmap Class is used for image display of drawing equipment , It can be used as a QPaintDevice object , It can also be loaded into a control , Usually labels or buttons , Used to display images on labels or buttons .
QPixmap The types of image files that can be read are BMP、GIF、JPG、JPEG、PNG、PBM、PGM、PPM、XBM 、XPM etc. .
QPixmap Class
| Method | describe |
|---|---|
| copy() | from QRect Object to QPixmap object |
| fromImage() | take Qlmage Object to QPixmap object |
| grabWidget() | Create a pixel map from a given widget |
| grabWindow() | Create a pixel map of data in the window |
| load() | Load the image file as QPixmap object |
| save() | take QPixmap Objects are saved as files |
| toImage() | take QPixmap Object to QImage object |
Case study ——QPixmap Use
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("../ Package resource files /pic/python.jpg"))
vbox=QVBoxLayout()
vbox.addWidget(lab1)
win.setLayout(vbox)
win.setWindowTitle("QPixmap Example ")
win.show()
sys.exit(app.exec_())

边栏推荐
- QT common operation collection
- TFRecord的Shuffle、划分和读取
- Feed流应用重构-架构篇
- 组件的传参
- Markdown extended syntax
- 传英特尔明年将采用台积电6nm EUV工艺
- Buuctf brushes eleven questions (05)
- [cloud native] deploy redis cluster in k8s
- The follow-up is coming. Whether it's OK without reference, let's make it clear to everyone at once!
- 紫光FPGA解决口罩难题!助力口罩生产全面提速
猜你喜欢

云计算服务主要安全风险及应对措施

20 character short domain name bypass replication

Kubernetes binary deployment - theoretical part

When type= 'number' is set in the input field, remove the up and down buttons behind it

4 轮拿下字节 Offer,面试题复盘

Cy3 fluorescent labeling antibody / protein Kit (10~100mg labeling amount)

Understanding and use of third-party library

What is the b+tree index of MySQL? How does the cluster index grow?

Cy3荧光标记抗体/蛋白试剂盒 (10~100mg标记量)

组件的传参
随机推荐
ADI、世健、骏龙科技共同捐赠230万元助力湖北抗疫
Fluorescence imaging of cle19 polypeptide in cells preparation of fluorescence quenching quantum dots of bovine serum albumin
Two dimensional code generation based on MCU and two dimensional code display on ink screen
Purple light FPGA solves the mask problem! Boost the overall speed-up of mask production
ADI, Shijian and Junlong technology jointly donated 2.3 million yuan to help fight the epidemic in Hubei
2022/6/9 exam summary
联发科携手三星推出全球首款支持Wi-Fi 6的8K电视
Invest 2.2 billion dollars! Geke micro 12 inch CIS manufacturing project settled in Shanghai Lingang
2022/6/5考试总结
PX4模块设计之十三:WorkQueue设计
Cy3荧光标记抗体/蛋白试剂盒 (10~100mg标记量)
格力口罩来了!KN95口罩只要5.5元一个!
技术生涯10年,那些让我心动的技术书
2022/3/10 exam summary
Preparation of peptide kc2s modified albumin nanoparticles / targeting peptide GX1 modified human serum albumin nanoparticles probe
Build your own website (22)
It's time to say goodbye gracefully to nullpointexception
【云原生】k8s 部署redis集群
中芯国际购买的ASML光刻机顺利进厂,但并未非EUV光刻机!
干货|语义网、Web3.0、Web3、元宇宙这些概念还傻傻分不清楚?(中)