Drawing Ellipse In PyQt5 With QPainter Class

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:

  1. Drawing Shapes: QPainter provides methods for drawing basic shapes such as lines, rectangles, ellipses and polygons.
  2. 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.
  3. Drawing Images: QPainter allows you to draw images onto paint devices, including loading images from files or using QPixmap objects.
  4. Custom Painting: QPainter enables you to perform custom painting by overriding the paintEvent() method of QWidget or subclassing QPaintDevice directly.
  5. 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

This code is a PyQt5 application that draws an ellipse shape on a window using QPainter.

  1. 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.
  2. 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.
  3. 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.
  4. 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

PyQt5 Drawing Ellipse With QPainter Class
PyQt5 Drawing Ellipse With QPainter Class

 

 

 

 

 

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

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×