PyQt - Drawing an Ellipse



An Ellipse is defined by two points, each called a focus. If any point on the Ellipse is taken, the sum of the distances to the focus points is constant. The size of the Ellipse is determined by the sum of these two distances. The sum of these distances is equal to the length of the major axis (the longest diameter of the ellipse). A circle is, in fact, a special case of an Ellipse.

ellipse

Properties of ellipse −

  • Center − It is a point where major and minor axis intersect.
  • Major axis − Define the longest diameter of an ellipse.
  • Minor axis − Define the shortest diameter of an ellipse.

Drawing a Ellipse in PyQt

Building an ellipse in PyQt needs various modules- QGraphics, QGraphicsView, QGraphicsEllipseItem, QApplication, QtWidgets, QBrush, QPainter, and, QPen. All these modules have convenience functions to operate the task of geometrical shape.

The shape of an ellipse can be formed using two methods- Ellipse() and drawEllipse(). Both these methods are used in different classes to build an ellipse.

Example 1

Following example illustrate the shape of ellipse using PyQt.

import sys
from PyQt6.QtWidgets import QGraphicsScene, QGraphicsView, QGraphicsEllipseItem, QApplication
from PyQt6.QtGui import QBrush, QColor
class Ellipse(QGraphicsEllipseItem):
   def __init__(self, x, y, width, height, color):
      super(Ellipse, self).__init__(x, y, width, height)
      # Create a QBrush with the specified color for filling the ellipse
      brush = QBrush(QColor(color))
      self.setBrush(brush)
class MyScene(QGraphicsScene):
   def __init__(self):
      super(MyScene, self).__init__()
      # Create an ellipse at position (50, 50) with a width of 100, height of 60, and blue color
      # Specify the color in RGB value
      ellipse = Ellipse(40, 40, 400, 200, "#789287")
      # Add the ellipse to the scene
      self.addItem(ellipse)

def main():
   app = QApplication(sys.argv)
   # Create a QGraphicsView
   view = QGraphicsView()
   # Create an instance of MyScene
   scene = MyScene()
   # Set the scene for the view
   view.setScene(scene)
   # Show the view
   view.show()
   # Execute the application
   sys.exit(app.exec())
if __name__ == '__main__':
   main()

Output

The above code produces the following output −

ellipse example one

Example 2

Following example illustrates the shape of ellipse using different code snippet. Here, QPainter, QBrush and QPen classes set the respective parameter to build the ellipse in color shape.

  • QPen(Qt.GlobalColor.green, 5, Qt.PenStyle.SolidLine) − The QPen() method is used for drawline line. So, Qt class create the object for styling line.
  • QBrush(Qt.GlobalColor.green, Qt.BrushStyle.SolidPattern) − The method QBrush() reference from QBrush class which provide color, texture, etc. for the shape.
from PyQt6 import QtWidgets
from PyQt6.QtGui import QPainter, QBrush, QPen
from PyQt6.QtCore import Qt
class MyWidget(QtWidgets.QWidget):
   def paintEvent(self, event):
      painter = QPainter(self)
      painter.setRenderHint(QPainter.RenderHint.Antialiasing)
      painter.setPen(QPen(Qt.GlobalColor.green, 5, Qt.PenStyle.SolidLine))
      painter.setBrush(QBrush(Qt.GlobalColor.green, Qt.BrushStyle.SolidPattern))
      painter.drawEllipse(40, 40, 400, 200)

app = QtWidgets.QApplication([])
widget = MyWidget()
widget.show()
app.exec()

Output

The above code produces the following output −

ellipse example two

Drawing a Cross Pattern Ellipse in PyQt

The cross pattern is a type of shape that is used in ellipse. Here, QBrush is the main class that fills the pattern of shape through the method Qpainter(). Then Qpainter has the built-in function drawEllipse() which draws the shape of an ellipse.

Example

Following example illustrate the code snippet of ellipse in cross-pattern style.

from PyQt6 import QtWidgets
from PyQt6.QtGui import QPainter, QBrush, QPen
from PyQt6.QtCore import Qt
class MyWidget(QtWidgets.QWidget):
   def paintEvent(self, event):
      painter = QPainter()
      painter.begin(self)
      painter.setRenderHint(QPainter.RenderHint.Antialiasing)
      painter.setPen(QPen(Qt.GlobalColor.red, 5, Qt.PenStyle.SolidLine))

      painter.setBrush(QBrush(Qt.GlobalColor.gray, Qt.BrushStyle.CrossPattern))
      painter.drawEllipse(40, 40, 400, 200)

app = QtWidgets.QApplication([])
widget = MyWidget()
widget.show()
app.exec()

Output

The above code produces the following output −

ellipse example three
Advertisements