In this PyQt6 lesson we are going to learn about PyQt6 QPainter Class, QPainter class has optimized functions to do drawing GUI programs. you can draw everything from simple lines to complex shapes like pies and chords. in this lesson we want to draw rectangle, ellipse, polygon and text.
Drawing Rectangle with QPainter
OK first of all we want to draw rectangle in pyqt6, now this is the complete source code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
from PyQt6.QtWidgets import QApplication, QWidget from PyQt6.QtGui import QIcon, QPainter, QPen, QBrush from PyQt6.QtCore import Qt import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("PyQt6 Rectangle - Geekscoders.com") self.setWindowIcon(QIcon("qt.png")) self.setGeometry(500,200, 500,400) def paintEvent(self, e): painter = QPainter(self) painter.setPen(QPen(Qt.GlobalColor.black, 5, Qt.PenStyle.DashDotLine)) painter.setBrush(QBrush(Qt.GlobalColor.green, Qt.BrushStyle.SolidPattern)) painter.drawRect(100, 15, 300, 100) app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec()) |
For drawing shapes in PyQt6 you need to add paintEvent() method and in that method you can draw your shapes.
We need to create the object of QPainter class.
1 |
painter = QPainter(self) |
Using this code we can set the pen and brush for our rectangle shape, if you are familiar with PyQt5, than here will be the biggest difference between PyQt5 and PyQt6.
1 2 3 4 5 |
painter.setPen(QPen(Qt.GlobalColor.black, 5, Qt.PenStyle.DashDotLine)) painter.setBrush(QBrush(Qt.GlobalColor.green, Qt.BrushStyle.SolidPattern)) |
And in here we can use drawRect() method for drawing of the rectangle in pyqt6, you need to add the x and y position also the width and height of the rectangle.
1 |
painter.drawRect(100, 15, 300, 100) |
Run the complete code and this is the result.
Drawing Ellipse with QPainter
Now we want to draw ellipse in pyqt6. this is the complete code for drawing ellipse.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
from PyQt6.QtWidgets import QApplication, QWidget from PyQt6.QtGui import QIcon, QPainter, QPen, QBrush from PyQt6.QtCore import Qt import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("PyQt6 Ellipse - Geekscoders.com") self.setWindowIcon(QIcon("qt.png")) self.setGeometry(500,200, 500,400) def paintEvent(self, e): painter = QPainter(self) painter.setPen(QPen(Qt.GlobalColor.yellow, 5, Qt.PenStyle.SolidLine)) painter.setBrush(QBrush(Qt.GlobalColor.green, Qt.BrushStyle.SolidPattern)) # painter.setBrush(QBrush(Qt.GlobalColor.red, # Qt.BrushStyle.DiagCrossPattern)) painter.drawEllipse(100, 100, 400, 200) app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec()) |
So in here you can use drawEllipse() function, and you need to give the x, y position, width and height of the ellipse.
1 |
painter.drawEllipse(100, 100, 400, 200) |
Run the complete code and this is the result.
Drawing Text with QPainter
OK Now we want to draw Text in pyqt6. this is the complete code for drawing Text.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
from PyQt6.QtWidgets import QApplication, QWidget from PyQt6.QtGui import QIcon, QPainter, QTextDocument from PyQt6.QtCore import QRect, Qt, QRectF import sys class Window(QWidget): def __init__(self): super().__init__() self.setWindowTitle("PyQt6 Text - Geekscoders.com") self.setWindowIcon(QIcon("qt.png")) self.setGeometry(500,200, 500,400) def paintEvent(self, e): painter = QPainter(self) painter.drawText(100, 100, "PyQt6 Course") rect = QRect(100, 150, 250, 25) painter.drawRect(rect) painter.drawText(rect, Qt.Alignment.AlignCenter, "PyQt6 Course - Geekscoders.com") document = QTextDocument() rect2 = QRectF(0, 0, 250, 250) document.setTextWidth(rect2.width()) document.setHtml("<b>Welcome to PyQt6 Course" "</b><i>Parwiz Forogh</i> \n <font size = '15' " "color='red'>Enjoy videos from the videos</font>") document.drawContents(painter, rect2) app = QApplication(sys.argv) window = Window() window.show() sys.exit(app.exec()) |
Also we have used html in our text using QTextDocument class.