PyQt - Drawing a Circle



A circle is a two-dimentional rounded figure where all point lies inside a circumference. A circumference is defined as measuring distance around the outside edge of a circle. Its application uses in various field- graphics, architecture, landscaping, transportation, etc.

circle

Drawing a Circle in PyQt

Circle is a geometrical concept which used in PyQt. The Widgets are parental root of any graphical user interface to draw the paintings. To draw a circle require multiple set of modules and built-in function that implement the specific tasks − color filled, border creation, plain shape, etc.

In this tutorial, we learn to draw different styles of circle in PyQt.

  • Dotted Circle − A Dotted Circle is a combination of multiple dots to draw a circle. Constructing a dotted shape uses three sets of parameters as integer precision. The first two sets are known for positioning and the other is diameter.
  • Plain Shape − A plain shape represent the simple shape of circle. This can be drawn using the method SimpleCircle(). This function accept three set of integer as parmeters- the first two parameter set as positioning and other is diameter.
  • Color Filled Circle − A circle filled with a background is called color filled circle. This can be drawn using the method FilledCircle.
  • Half Circle − The Half circle comparatively similar to the shape of an arc. Therefore, we use drawArc()function to display the half circle.

Example 1

Following example illutrates the code snippet of dotted circle using PyQt. Here, we use the class QPen that set the parameter as Qt.PenStyle.DotLine to create a styling effect(dot).

import sys
from PyQt6.QtWidgets import QGraphicsScene, QGraphicsView, QGraphicsEllipseItem, QApplication
from PyQt6.QtCore import Qt
from PyQt6.QtGui import QPen, QColor
# Custom QGraphicsEllipseItem for a dotted circle
class DottedCircle(QGraphicsEllipseItem):
   def __init__(self, x, y, diameter):
      super(DottedCircle, self).__init__(x, y, diameter, diameter)
      # Set the pen style to create a dotted circle
      pen = QPen(Qt.PenStyle.DotLine)
      self.setPen(pen)

# Custom QGraphicsScene
class MyScene(QGraphicsScene):
   def __init__(self):
      super(MyScene, self).__init__()
      # Create a dotted circle at position (50, 50) with a diameter of 100
      dotted_circle = DottedCircle(50, 50, 100)
      # Add the circle to the scene
      self.addItem(dotted_circle)

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 −

dotted circle

Example 2

Following example illustrate the code snippet of Plain Circle in PyQt.

import sys
from PyQt6.QtWidgets import QGraphicsScene, QGraphicsView, QGraphicsEllipseItem, QApplication
from PyQt6.QtCore import Qt
class SimpleCircle(QGraphicsEllipseItem):
   def __init__(self, x, y, diameter):
      super(SimpleCircle, self).__init__(x, y, diameter, diameter)

class MyScene(QGraphicsScene):
   def __init__(self):
      super(MyScene, self).__init__()

      # Create a simple circle at position (50, 50) with a diameter of 100
      simple_circle = SimpleCircle(50, 50, 100)

      # Add the circle to the scene
      self.addItem(simple_circle)

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 −

plain_circle

Example 3

Following example illustrate the code snippet of color filled circle using PyQt. Here, we apply the method FilledCircle() that used to specify the points with a color value.

import sys
from PyQt6.QtWidgets import QGraphicsScene, QGraphicsView, QGraphicsEllipseItem, QApplication
from PyQt6.QtGui import QBrush, QColor
class FilledCircle(QGraphicsEllipseItem):
   def __init__(self, x, y, diameter, color):
      super(FilledCircle, self).__init__(x, y, diameter, diameter)

      # Create a QBrush with the specified color
      brush = QBrush(QColor(color))
      self.setBrush(brush)
class MyScene(QGraphicsScene):
   def __init__(self):
      super(MyScene, self).__init__()
      # Create a filled circle at position (50, 50) with a diameter of 100 and red color
      # Specify color using hexadecimal RGB value
      filled_circle = FilledCircle(50, 50, 100, "#FF0000")  
      # Add the filled circle to the scene
      self.addItem(filled_circle)
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 −

plain_shape_circle

Example 4

Following example illustrate the code of half circle using PyQt.

from PyQt6 import QtWidgets, QtGui, QtCore
from PyQt6.QtGui import QPainter
class MyWidget(QtWidgets.QWidget):
   def paintEvent(self, event):
      painter = QPainter(self)
      painter.setRenderHint(QtGui.QPainter.RenderHint.Antialiasing)
      painter.setPen(QtCore.Qt.GlobalColor.black)
      painter.setBrush(QtCore.Qt.GlobalColor.white)
      painter.drawArc(110, 80, 310, 310, 0 * 26, 100 * 26)

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

Output

The above code produces the following output −

arc
Advertisements