PyQt6 Tutorials
About Lesson

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.

 

 

 

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.

 

 

 

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.

 

 

 

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.

 

 

Run the complete code and this is the result.

PyQt6 QPainter Class
PyQt6 QPainter Class

 

 

 

Drawing Ellipse with QPainter

Now we want to draw ellipse in pyqt6. this is the complete code for drawing ellipse.

 

 

 

So in here you can use drawEllipse() function, and you need to give the x, y position, width and height of the ellipse.

 

 

 

Run the complete code and this is the result.

PyQt6 Creating Ellipse with QPainter
PyQt6 Creating Ellipse with QPainter

 

 

 

Drawing Text with QPainter

OK Now we want to draw Text in pyqt6. this is the complete code for drawing Text.

 

 

 

Also we have used html in our text using QTextDocument class.

PyQt6 QPainter Class Drawing Text
PyQt6 QPainter Class Drawing Text

 

Thank you for visiting my website! If you enjoyed the free courses, please consider supporting my works on Patreon.
Exercise Files
No Attachment Found
No Attachment Found