PyQt - Geometric Transformation



The Geometric transformation refers to understanding the dynamic aspect of a geometrical figure. The geometric figure is used to describe the architectural behavior i.e. used in a wide range of applications such as resizing, positioning, image processing, etc.

Mostly three types of painting are used in geometric transformation − arrow, arc, and curve. All these figures are mathematically implemented in the graph.

geometric transformation

The classes QPainter and QPainterPath operate the task of painting operation which can be reused and construted for graphical shape.

Drawing an arrow in PyQt

An arrow is a direct line segement which indicate the direction. To draw an arc, use the drawLine() method for three times- one will be horizontal line and rest two are head and down of an arrow.

Example

Following example to illustrate an arrow using PyQt.

from PyQt6 import QtWidgets, QtGui, QtCore
class MyWidget(QtWidgets.QWidget):
   def paintEvent(self, event):
      painter = QtGui.QPainter(self)
      painter.setRenderHint(QtGui.QPainter.RenderHint.Antialiasing)
      painter.setPen(QtGui.QColor(QtCore.Qt.GlobalColor.green))
      painter.setBrush(QtGui.QColor(QtCore.Qt.GlobalColor.white))

      # Draw lines
      # Straight line
      painter.drawLine(600, 100, 100, 100)
      # Head line
      painter.drawLine(250, 150, 100, 100)
      # Down line
      painter.drawLine(250, 50, 100, 100)

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

Output

The above code produces the following output −

arrow example

Drawing an arc in PyQt

The arc is defined by the circumference of a circle. The method drawArc() is used to build an arc. In addition, the method setPen() and setBrush() are set for coloring the graph.

Syntax

Following syntax to draw an arc using PyQt −

drawArc(x_axis, y_axis, width, length, startAngle, spanAngle)

Parameters

  • x_axis and y-axis − The axes represent the upper-left corner of the rectangle that bound the ellipse from which the arc has been drawn.
  • width and height − This determines the size of an arc.
  • startAngle − It is an angle that is measured clockwise from the x-axis to the starting point of an arc.
  • spanAngle − This angle is measured in degree and clockwise from the startAngle parameter to the ending point of an arc.

Example

Following example to illustrate an arc 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(QtGui.QColor(QtCore.Qt.GlobalColor.black))
      painter.setBrush(QtGui.QColor(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

Drawing a curve in PyQt

A line that is not straight is called a curve. Here, QPainterPath class is called using the function drawPath() because the variable path is associated with the function cubicTo() to calculate the curve using its parameters.

Syntax

Following the main syntax used to calculate the points of curve using PyQt −

cubicTo(control_point_x1, control_point_y1, control_point_x2, control_point_y2, 
end_point_x, end_point_y)

Example

Following example illustrate the code snippet of curve using PyQt.

from PyQt6 import QtWidgets, QtGui, QtCore
from PyQt6.QtGui import QPainter, QPainterPath
class MyWidget(QtWidgets.QWidget):
   def paintEvent(self, event):
      painter = QPainter()
      path = QPainterPath()
      painter.begin(self)
      painter.setRenderHint(QtGui.QPainter.RenderHint.Antialiasing)
      painter.setPen(QtGui.QColor(QtCore.Qt.GlobalColor.gray))
      painter.setBrush(QtGui.QColor(QtCore.Qt.GlobalColor.gray))
      # start point
      path.moveTo(40, 40)
      # points calculation using cubicTo() method
      path.cubicTo(40, 40, 150, 250, 250, 40)
      painter.drawPath(path)
      painter.end()

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

Output

The above code produces the following output −

curve
Advertisements