In this article i want to show you Drawing of Ellipse in PyQt5 With QPainter Class , as i have mentioned in my previous articles, for Drawing in PyQt5 we can use QPainter class. before this we had a post on Drawing Of Rectangle In PyQt5 , so first of all let me just tell a few words about QPainter class in PyQT5.
What is QPainter in PyQt5?
In PyQt5, QPainter is a class that provides high-level painting functionality for rendering graphics onto different paint devices, such as widgets, images and printers. It allows you to draw and manipulate 2D graphics, including lines, shapes, text, and images with different styles, colors, and transformations.
QPainter is commonly used for custom drawing and rendering in PyQt5 applications. You can use it to create custom widgets, draw diagrams, charts, or graphs, and perform different graphical operations.
This is a brief overview of what you can do with QPainter in PyQt5:
- Drawing Shapes: QPainter provides methods for drawing basic shapes such as lines, rectangles, ellipses and polygons.
- Drawing Text: You can draw text onto paint devices using QPainter. This includes drawing single-line and multi-line text, specifying fonts, styles and alignment.
- Drawing Images: QPainter allows you to draw images onto paint devices, including loading images from files or using QPixmap objects.
- Custom Painting: QPainter enables you to perform custom painting by overriding the
paintEvent()
method of QWidget or subclassing QPaintDevice directly. - Transformations: QPainter supports various transformations such as translations, rotations, scaling, and shearing, which can be applied to drawings.
Drawing Ellipse In PyQt5 With QPainter Class
So now this is the source code for Drawing Ellipse In PyQt5 With QPainter Class
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 |
from PyQt5 import QtGui from PyQt5.QtWidgets import QApplication, QMainWindow import sys from PyQt5.QtGui import QPainter, QPen, QBrush from PyQt5.QtCore import Qt class Window(QMainWindow): def __init__(self): super().__init__() self.title = "Codeloop - Drawing Ellipse" self.top = 200 self.left = 500 self.width = 600 self.height = 400 self.InitWindow() def InitWindow(self): # Initialize window properties self.setWindowIcon(QtGui.QIcon("icon.png")) self.setWindowTitle(self.title) self.setGeometry(self.left, self.top, self.width, self.height) self.show() def paintEvent(self, event): # Override paint event to draw custom shapes painter = QPainter(self) # Create QPainter object painter.setPen(QPen(Qt.black, 5, Qt.SolidLine)) # Set pen color, width, and style painter.setBrush(QBrush(Qt.red, Qt.SolidPattern)) # Set brush color and pattern painter.drawEllipse(100, 100, 400, 200) # Draw an ellipse # Main entry point if __name__ == "__main__": App = QApplication(sys.argv) window = Window() sys.exit(App.exec()) |
This code is a PyQt5 application that draws an ellipse shape on a window using QPainter.
- Imports:
- It imports necessary modules from PyQt5 for GUI development.
- QtGui module for GUI functionality.
- QApplication and QMainWindow classes for creating the application window.
- QPainter, QPen, QBrush, and Qt for drawing shapes and setting their attributes.
- sys module for system-specific parameters and functions.
- Window Class:
- Window class inherits from QMainWindow.
- It initializes window properties such as title, position, and size in the constructor.
- InitWindow() method sets window icon, title, and geometry, and displays the window.
- Paint Event:
- The paintEvent() method is overridden to draw custom shapes on the window.
- Inside paintEvent(), a QPainter object is created.
- setPen() method sets the pen properties such as color, width, and style.
- setBrush() method sets the brush properties such as color and pattern.
- drawEllipse() method draws an ellipse shape on the window with specified dimensions and location.
- Main Function:
- It creates an instance of QApplication and Window class.
- Executes the application using sys.exit(App.exec()).
Run the complete code and this will be the result
FAQs:
What is QPainter in PyQt5?
QPainter is a class in PyQt5 that provides high-level painting functionality for rendering graphics onto different paint devices, such as widgets, images, and printers. It allows drawing and manipulating 2D graphics, including lines, shapes, text, and images with different styles, colors and transformations.
What is the purpose of the paintEvent() method in PyQt5?
- paintEvent() method in PyQt5 is used to handle painting events. It is called whenever the widget needs to be repainted.
- By overriding the paintEvent() method, you can perform custom painting operations such as drawing shapes, text, or images onto the widget.
Subscribe and Get Free Video Courses & Articles in your Email