PyQt5 Tutorials
About Lesson

In this Python PyQt5 Tutorial we are going to learn about Python PyQt5 QPainter, so PyQt5 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.

 

1: Drawing Rectangle with QPainter

OK first of all we want to draw rectangle in pyqt5, now this is the complete source code.

 

 

 

These are our window requirements like window title, window icon, width and height of the window, x and y position of the window.

 

For drawing shapes with QPainter class in Python PyQt5 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.

 

 

And in here we can use drawRect() method for drawing of the rectangle in pyqt5, you need to add the x and y position also the width and height of the rectangle.

 

 

 

Run the code this is the result.

PyQt5 Tutorial - Working with QPainter class in PyQt
PyQt5 Tutorial – Working with QPainter class in PyQt

 

2: Drawing Ellipse with QPainter

Now we want to draw ellipse in pyqt5. 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.

PyQt5 QPainter Ellipse
PyQt5 QPainter Ellipse

 

3: Drawing Text with QPainter

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

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

 

 

 

Run the complete code and this will be the result.

PyQt5 QPainter Drawing Text
PyQt5 QPainter Drawing Text

 

 

4: Drawing Polygon with QPainter

Let’s create polygon in python pyqt5, this is the complete source code for drawing polygon.

 

 

For drawing polygon the first thing you need to create some points using QPolygon.

 

 

After that you can use drawPolygon() method and you need to add your points in the method.

 

 

Run the complete code and this is the result.

PyQt5 QPainter Drawing Polygon
PyQt5 QPainter Drawing Polygon