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.

 

Join PyQt5 Full Course for Free

PyQt5 Course

 

 

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

 

 

 

Also you can watch the complete video for this article

 

 

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Codeloop
Share via
Copy link
Powered by Social Snap
×