- PyQt5 - Discussion
- PyQt5 - Useful Resources
- PyQt5 - Quick Guide
- PyQt5 - QPixmap Class
- PyQt5 - QClipboard
- PyQt5 - BrushStyle Constants
- PyQt5 - Drawing API
- PyQt5 - Database Handling
- PyQt5 - Drag & Drop
- PyQt5 - Multiple Document Interface
- PyQt5 - QMessageBox
- PyQt5 - QDialog Class
- PyQt5 - Basic Widgets
- PyQt5 - Layout Management
- PyQt5 - Signals & Slots
- PyQt5 - Using Qt Designer
- PyQt5 - Major Classes
- PyQt5 - Hello World
- PyQt5 - What’s New
- PyQt5 - Introduction
- PyQt5 - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
PyQt5 - Drawing API
All the QWidget classes in PyQt are sub classed from QPaintDevice class. A QPaintDevice is an abstraction of two dimensional space that can be drawn upon using a QPainter. Dimensions of paint device are measured in pixels starting from the top-left corner.
QPainter class performs low level painting on widgets and other paintable devices such as printer. Normally, it is used in widget’s paint event. The QPaintEvent occurs whenever the widget’s appearance is updated.
The painter is activated by calpng the begin() method, while the end() method deactivates it. In between, the desired pattern is painted by suitable methods as psted in the following table.
Sr.No. | Methods & Description |
---|---|
1 |
begin() Starts painting on the target device |
2 |
drawArc() Draws an arc between the starting and the end angle |
3 |
drawElppse() Draws an elppse inside a rectangle |
4 |
drawLine() Draws a pne with endpoint coordinates specified |
5 |
drawPixmap() Extracts pixmap from the image file and displays it at the specified position |
6 |
drwaPolygon() Draws a polygon using an array of coordinates |
7 |
drawRect() Draws a rectangle starting at the top-left coordinate with the given width and height |
8 |
drawText() Displays the text at given coordinates |
9 |
fillRect() Fills the rectangle with the QColor parameter |
10 |
setBrush() Sets a brush style for painting |
11 |
setPen() Sets the color, size and style of pen to be used for drawing |
Example
In the following code, various methods of PyQt s drawing methods are used.
import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class Example(QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): self.text = "hello world" self.setGeometry(100,100, 400,300) self.setWindowTitle( Draw Demo ) self.show() def paintEvent(self, event): qp = QPainter() qp.begin(self) qp.setPen(QColor(Qt.red)) qp.setFont(QFont( Arial , 20)) qp.drawText(10,50, "hello Python") qp.setPen(QColor(Qt.blue)) qp.drawLine(10,100,100,100) qp.drawRect(10,150,150,100) qp.setPen(QColor(Qt.yellow)) qp.drawElppse(100,50,100,50) qp.drawPixmap(220,10,QPixmap("pythonlogo.png")) qp.fillRect(20,175,130,70,QBrush(Qt.SopdPattern)) qp.end() def main(): app = QApppcation(sys.argv) ex = Example() sys.exit(app.exec_()) if __name__ == __main__ : main()
The above code produces the following output −
Advertisements