Python PyQt5 Drawing Rectangle With QPainter Class

In this Python PyQt5 article i want to show you Drawing of Rectangle With QPainter Class , and also we  are going to talk about QPainter Class in PyQt5 . so first of all let me talk about QPainter class.

 

 

Also you can read more Python GUI articles in the below links

 

 

What is Python PyQt5 QPainter Class ?

The QPainter class in PyQt5 is a powerful and flexible class that provides different 2D painting functions to create custom graphics and designs in PyQt5 application. It allows you to paint on widgets and images using a variety of pens, brushes, and other tools. You can use it to draw lines, shapes, text, images, and gradients, and apply transformations such as scaling, rotation, and translation.

 

 

This is simple example

In the above example we have create MyWidget class that inherits from QWidget. after that we set the geometry and window title of the widget in the constructor. after that we override the paintEvent() method which is called whenever the widget needs to be repainted. in this method we have created QPainter object that is associated with the widget by passing self as an argument. we set the pen color to red using setPen() and draw a rectangle using drawRect().

and lastly we create an instance of MyWidget and show it using the show() method, as well as running the Qt event loop using app.exec_(). when the window appears, you should see red rectangle drawn on it.

 

 

Run the code and this will be the result

Python PyQt5 Drawing Rectangle With QPainter Class
Python PyQt5 Drawing Rectangle With QPainter Class

 

 

 

Now let’s create another example, now this is the complete code for drawing a rectangle

 

 

 

This is our main Window class that extends from QMainWindow. and in the constructor of the class we need to initialize some requirements of the window . also we have called our initWindow() method in here.

 

 

 

After that we are going to create our InitWindow() method. we set our window title, window icon and window geometry. also we need to show our window.

 

 

 

This is the paintEvent() method and it is built in method in QPainter class, and we are going to draw our Reactangle in this method. first we have created the object of QPainter class, after that we have set the pen and also brush for the Rectangle and at the end we have drawn the Rectangle.

 

 

 

Also every PyQt5 application must create an application object. 

 

 

Finally, we enter the mainloop of the application. The event handling starts from this point. 

 

 

 

Run the complete code and this will be the result

Python PyQt5 Drawing Rectangle With QPainter Class
Python PyQt5 Drawing Rectangle With QPainter Class

 

 

 

QPainter Class Functionalities in PyQt5

You can use QPainter class to draw different elements on a widget. 

 

  1. Drawing Shapes:
  • QPainter offers methods to draw basic geometric shapes such as rectangles, ellipses, polygons, and circles. For example, drawRect(), drawEllipse(), drawPolygon() and drawCircle() methods can be used to draw these shapes. These methods take parameters like position, size and style to customize the appearance of the shapes.

 

  1. Drawing Lines:
  • Lines can be drawn using the drawLine() method of QPainter. This method requires specifying the start and end points of the line. Additionally, QPainter provides options to customize the line style, thickness, and color using methods like setPen() and setBrush().

 

  1. Drawing Text:
  • Text can be drawn on widgets using the drawText() method of QPainter. This method allows specifying the position, font and color of the text to be drawn. PyQt5 provides QFont class to set the font properties such as font family, size, weight, and style. QPainter also supports drawing multiline text and text alignment options.

 

  1. Drawing Images:
  • QPainter enables drawing images on widgets using the drawImage() method. This method requires specifying the position and size of the image to be drawn. PyQt5 provides QImage and QPixmap classes to load and manipulate image data. Images can be loaded from files or created dynamically from raw image data.

 

 

 

Let’s demonstrate these functionalities with a practical example:

In this example, we have created a custom widget MyWidget that inherits from QWidget. We override the paintEvent() method to perform custom painting using QPainter. Inside paintEvent(), we use different QPainter methods to draw shapes, lines, text and images on the widget. And lastly we create an instance of MyWidget and display it using the QApplication event loop.

 

 

 

Run the code and this will be the result

Python PyQt5 Drawing Rectangle With QPainter Class
Python PyQt5 Drawing Rectangle With QPainter Class

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×