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
- Kivy GUI Development Tutorials
- TKinter GUI Development Tutorials
- Pyside2 GUI Development
- wxPython GUI Development Tutorials
- PyQt5 GUI Development Tutorials
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
import sys from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtGui import QPainter, QColor class MyWidget(QWidget): def __init__(self): super().__init__() self.setGeometry(100, 100, 300, 200) self.setWindowTitle('Drawing in Codeloop') def paintEvent(self, event): painter = QPainter(self) painter.setPen(QColor(255, 0, 0)) painter.drawRect(50, 50, 100, 100) if __name__ == '__main__': app = QApplication(sys.argv) widget = MyWidget() widget.show() sys.exit(app.exec_()) |
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
Now let’s create another example, now this is the complete code for drawing a rectangle
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 37 38 39 40 41 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5.QtGui import QPainter, QBrush, QPen from PyQt5.QtCore import Qt import sys class Window(QMainWindow): def __init__(self): super().__init__() self.title = "PyQt5 Drawing Rectangle" self.top = 100 self.left = 100 self.width = 680 self.height = 500 self.InitWindow() def InitWindow(self): self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.top, self.left, self.width, self.height) self.show() def paintEvent(self, e): painter = QPainter(self) painter.setPen(QPen(Qt.black, 5, Qt.SolidLine)) #painter.setBrush(QBrush(Qt.red, Qt.SolidPattern)) painter.setBrush(QBrush(Qt.green, Qt.DiagCrossPattern)) painter.drawRect(100, 15, 400,200) App = QApplication(sys.argv) window = Window() sys.exit(App.exec_()) |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
class Window(QMainWindow): def __init__(self): super().__init__() self.title = "PyQt5 Drawing Rectangle" self.top = 100 self.left = 100 self.width = 680 self.height = 500 self.InitWindow() |
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.
1 2 3 4 5 |
def InitWindow(self): self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.top, self.left, self.width, self.height) self.show() |
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.
1 2 3 4 5 6 7 |
def paintEvent(self, e): painter = QPainter(self) painter.setPen(QPen(Qt.black, 5, Qt.SolidLine)) #painter.setBrush(QBrush(Qt.red, Qt.SolidPattern)) painter.setBrush(QBrush(Qt.green, Qt.DiagCrossPattern)) painter.drawRect(100, 15, 400,200) |
Also every PyQt5 application must create an application object.
1 |
App = QApplication(sys.argv) |
Finally, we enter the mainloop of the application. The event handling starts from this point.
1 2 |
window = Window() sys.exit(App.exec()) |
Run the complete code and this will be the result
Also you can watch the complete video for this article
Subscribe and Get Free Video Courses & Articles in your Email