PyQt - Drawing a Line



A line is a one-dimensional geometrical shape that has length but no width. The line extends infinitely in both directions. A line can be used in equations, graphs of lines, etc.

In PyQt, we can draw a line using drawline() method. The drawline() method draws a line from one point to another as per the coordinates specified.

line

The above figures represent the line in three different forms- Horizontal, vertical, and diagonal.

  • Horizontal Line − The horizontal line is also termed a straight path line that starts the point from left to right or from right to left.
  • Vertical Line − The vertical line is opposite to Horizontal lines that start from the top and go either from bottom to top. In geometry, we know a cartesian coordinate system is used and has two axes- x-axis and y-axis.
  • Diagonal Line − The diagonal is neither a horizontal nor a vertical line.

In this tutorial, we learn how to draw a line in PyQt.

Following steps to draws a line −

Step 1 − Start with a painter object i.e. QPainter(self).

Step 2 − Next, we perform the method setRenderHints() to get the rendering hints flag for the painter object.

Step 3 − Set the pen for line i.e. setpen().

Step 4 − Set the brush style for a line using the setBrush() method.

Step 5 − Use the method drawline() to set the coordinates points.

Step 6 − Finally, we get the resultant output of a line.

Note that, rendering hints are parameter that enhances the rendering quality and performance of graphical context.

Example 1

In this example, the line is drawn horizontally from the point (400, 100) to the point (100, 100) using the drawline() method. These two points create a visual representation of a horizontal line.

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

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

Output

The above code produces the following output-

Horizontal line

Example 2

In this example, we can draw a vertical line using drawline() method that accepts the coordinates point as a parameter. The starting point is represented using (100, 100) and end point with (100, 400). We assumed that the line is vertical as the x-coordinates are the same (100), and the y-coordinates indicate the vertical extent of the line.

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.green)
      painter.setBrush(QtCore.Qt.GlobalColor.white)
      painter.drawLine(100, 100, 100, 400)

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

Output

The above code produces the following output −

Vertical line

Example 3

In this example, we illustrate the line in a diagonal direction using PyQt. Here, the point starts from the left corner (0,0) of a polygon and ends at the right corner (300, 300). It is observed that the point initially starts from the origin, i.e., the left corner, and to make the line diagonal, the coordinate pairs increase equally.

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.green)
      painter.setBrush(QtCore.Qt.GlobalColor.white)
      painter.drawLine(0, 0, 300, 300)

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

Output

The above code produces the following output.

Diagonal Line
Advertisements