
- PyQt - Home
- PyQt - Introduction
- PyQt - Environment
- PyQt - Hello World
- PyQt - Major Classes
- PyQt - Using Qt Designer
- PyQt - Meta Objects
- PyQt Signals & Slots
- PyQt - Signals and Slots
- PyQt - Support and Signals
- PyQt - Unbound and Bound Signals
- PyQt - New Signals with PyQtSignal
- PyQt - Connecting, Disconnecting, & Emitting Signals
- PyQt - Slot decorator
- PyQt - Slot Connection
- PyQt Layouts
- PyQt - Layout Management
- PyQt - QBoxLayout
- PyQt - QGridLayout
- PyQt - QFormLayout
- PyQt - QHBoxLayout
- PyQt - QVBoxLayout
- PyQt - QStackedLayout
- PyQt - QGraphicsGridLayout
- PyQt - QGraphicsAnchorLayout
- PyQt - QGraphicsLayout
- PyQt - QGraphicsLinearLayout
- PyQt Basic Widgets
- PyQt - Basic Widgets
- PyQt - Qlabel Widget
- PyQt - QLineEdit Widget
- PyQt - QPushButton Widget
- PyQt - QRadioButton Widget
- PyQt - QCheckBox Widget
- PyQt - QComboBox Widget
- PyQt - QSpinBox Widget
- PyQt - QMessageBox
- PyQt - QDialogButtonBox Widget
- PyQt - QFontComboBox Widget
- PyQt - QDoubleSpinBox Widget
- PyQt - QToolBox Widget
- PyQt - QMenuBar, QMenu & Qaction Widgets
- PyQt - QToolTip
- PyQt - QInputDialog Widget
- PyQt - QFontDialog Widget
- PyQt - QDialog Widget
- PyQt - QFileDialog Widget
- PyQt - QTab Widget
- PyQt - QSplitter Widget
- PyQt - QDock Widget
- PyQt - QStatusBar Widget
- PyQt - QTabBar
- PyQt - QList Widget
- PyQt - QScrollBar Widget
- PyQt - QProgressBar
- PyQt - QCalendar Widget
- PyQt - QMessageBox Widget
- PyQt - QPlainTextEdit
- PyQt - QDateEdit
- PyQt - QDateTimeEdit
- PyQt - QTimeEdit
- PyQt - QTextEdit
- PyQt - QTextBrowser
- PyQt - QScrollArea
- PyQt - Drag and Drop
- PyQt - Multiple Document Interface
- PyQt - QDialog Class
- PyQt Views
- PyQt - QColumnView
- PyQt - QTableView
- PyQt Drawing API
- PyQt - Drawing API
- PyQt - Drawing a Line
- PyQt - Drawing a Rectangle
- PyQt - Drawing a Triangle
- PyQt - Drawing a Circle
- PyQt - Drawing a Ellipse
- PyQt - Drawing a Polygon
- PyQt - Geometric Transformation
- PyQt - Drawing Effect
- PyQt Groups
- PyQt - QButtonGroup
- PyQt - QGroupBox
- PyQt Effects
- PyQt - Effects
- PyQt - Opacity Effect
- PyQt - QGraphicsBlur Effect
- PyQt - QGraphicsColorize Effect
- PyQt - QGraphicsDropShadow Effect
- PyQt Events
- PyQt - Event Handling
- PyQt - Drag & Drop Events
- PyQt - File Open Event
- PyQt - Action Event
- PyQt - Hide Event
- PyQt - Resize Event
- PyQt Database
- PyQt - Database Handling
- PyQt Essentials
- PyQt - BrushStyle Constants
- PyQt - QClipboard
- PyQt - QPixmap Class
- PyQt Useful Resources
- PyQt - Quick Guide
- PyQt - Useful Resources
- PyQt - Discussion
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.

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-

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 −

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.
