Basic 2D Drawing with QPainter in PySide6
Course Title: PySide6 Application Development
Section Title: Working with Graphics and Animations
Topic: Basic 2D drawing with QPainter
Overview
In this topic, we will explore the basics of 2D drawing using QPainter in PySide6. QPainter is a powerful and versatile class that allows you to perform a wide range of drawing operations, from simple shapes and lines to complex graphics and text rendering.
QPainter Basics
Before we dive into the code, let's cover some basic concepts about QPainter:
QPainter
is a class that provides a way to perform 2D painting on aQWidget
or other paint device.QPainter
uses aQPaintDevice
to render graphics. Examples ofQPaintDevice
includeQWidget
,QImage
, andQPrinter
.QPainter
has several modes, including drawing shapes, lines, and text.
Basic 2D Drawing
To perform basic 2D drawing with QPainter, you'll need to follow these steps:
- Create a
QPainter
object and pass aQPaintDevice
(such as aQWidget
) to its constructor. - Set the pen and brush colors using
setPen()
andsetBrush()
. - Use various drawing functions to draw shapes, lines, and text.
Here's an example code snippet that demonstrates basic 2D drawing with QPainter:
import sys
from PySide6.QtCore import Qt
from PySide6.QtGui import QPainter, QPen, QBrush, QFont
from PySide6.QtWidgets import QWidget, QApplication
class DrawingWidget(QWidget):
def __init__(self):
super().__init__()
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen(QPen(Qt.black, 2))
painter.setBrush(QBrush(Qt.blue))
painter.drawRect(10, 10, 200, 200)
painter.drawText(20, 30, "Hello, World!")
if __name__ == "__main__":
app = QApplication(sys.argv)
widget = DrawingWidget()
widget.resize(400, 300)
widget.show()
sys.exit(app.exec())
In this example, we create a DrawingWidget
class that inherits from QWidget
. In the paintEvent()
method, we create a QPainter
object and set the pen and brush colors. We then use the drawRect()
function to draw a rectangle and the drawText()
function to draw text.
Drawing Shapes
QPainter provides several functions for drawing various shapes, including:
drawRect()
: Draws a rectangle.drawEllipse()
: Draws an ellipse.drawLine()
: Draws a line.drawPolyline()
: Draws a polyline.drawPolygon()
: Draws a polygon.
Here's an example code snippet that demonstrates drawing various shapes:
import sys
from PySide6.QtCore import Qt
from PySide6.QtGui import QPainter, QPen, QBrush
from PySide6.QtWidgets import QWidget, QApplication
class ShapeWidget(QWidget):
def __init__(self):
super().__init__()
def paintEvent(self, event):
painter = QPainter(self)
painter.setPen(QPen(Qt.black, 2))
# Draw a rectangle
painter.setBrush(QBrush(Qt.red))
painter.drawRect(10, 10, 100, 100)
# Draw an ellipse
painter.setBrush(QBrush(Qt.green))
painter.drawEllipse(150, 150, 100, 100)
# Draw a line
painter.setPen(QPen(Qt.blue, 5))
painter.drawLine(10, 250, 300, 250)
# Draw a polyline
points = [(10, 300), (50, 350), (300, 300)]
painter.setPen(QPen(Qt.black, 2))
painter.drawPolyline(points)
# Draw a polygon
points = [(10, 400), (50, 350), (300, 300), (200, 400)]
painter.setPen(QPen(Qt.black, 2))
painter.drawPolygon(points)
if __name__ == "__main__":
app = QApplication(sys.argv)
widget = ShapeWidget()
widget.resize(400, 500)
widget.show()
sys.exit(app.exec())
In this example, we create a ShapeWidget
class that inherits from QWidget
. In the paintEvent()
method, we create a QPainter
object and draw various shapes using the corresponding functions.
Summary
In this topic, we covered the basics of 2D drawing with QPainter in PySide6. We explored the QPainter
class and its various functions for drawing shapes, lines, and text. We also demonstrated how to use these functions in example code snippets.
Key Takeaways
- QPainter is a powerful class for performing 2D drawing operations.
- Use
QPainter
with aQPaintDevice
to render graphics. - Set pen and brush colors using
setPen()
andsetBrush()
. - Use various drawing functions to draw shapes, lines, and text.
Additional Resources
We hope this topic has helped you understand the basics of 2D drawing with QPainter in PySide6. If you have any questions or need further clarification, feel free to leave a comment below.
In the next topic, we will cover packaging PySide6 applications for distribution using PyInstaller and fbs.
Images

Comments