python logo

pyqt5 qpainter


Python hosting: Host, run, and code Python in the cloud!

Leveraging the power of QPainter in PyQt5, you can seamlessly add pixel-level drawings to your applications. Unlike most other widgets in PyQt5, the QPainter widget specializes in pixel manipulation. Here’s a guide on how to harness this widget using Python.

To integrate this widget into your PyQt5 application, PyQt5.QtGui is essential as it houses the QPainter alongside other utilities like qpen and qcolor.

Related Course:

Guide to Using the QPainter Widget
Setting a window’s background is quite straightforward:

# Configure window background color
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), Qt.white)
self.setPalette(p)

To add individual pixels, employ the drawPoint(x, y) method.

PyQt5 qpainter example

Step-by-step PyQt5 QPainter Illustration
The following sample demonstrates pixel rendering within the QPainter widget:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QLabel
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import Qt
import random

class App(QMainWindow):
def __init__(self):
super().__init__()
self.title = 'PyQt drawing app - pythonspot.com'
self.left = 10
self.top = 10
self.width = 440
self.height = 280
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
# Set window background color
self.setAutoFillBackground(True)
p = self.palette()
p.setColor(self.backgroundRole(), Qt.white)
self.setPalette(p)
# Initiate paint widget and paint
self.m = PaintWidget(self)
self.m.move(0,0)
self.m.resize(self.width,self.height)
self.show()
class PaintWidget(QWidget):
def paintEvent(self, event):
qp = QPainter(self)
qp.setPen(Qt.black)
size = self.size()
for i in range(1024):
x = random.randint(1, size.width()-1)
y = random.randint(1, size.height()-1)
qp.drawPoint(x, y)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())

Resources:
Acquire PyQT5 Examples

Previous Article | Next Article






Leave a Reply: