dev/qt/ PysideWidgetPainting1


Back Example2

Draws a random rectangle when the user clicks the mouse. Paints onto a pixmap and then paintEvent causes that pixmap to be drawn on the widget.

#!/usr/bin/env python3
import random

from PySide6.QtWidgets import (
    QWidget,
    QMainWindow,
    QApplication,
)
from PySide6.QtCore import (
    QPoint,
    QRect,
    Qt,
    QDir,
    Slot,
    QStandardPaths,
)
from PySide6.QtGui import (
    QMouseEvent,
    QPaintEvent,
    QPen,
    QBrush,
    QPainter,
    QColor,
    QPixmap,
)
import sys

class MyWidget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.w, self.h = 680, 480
        self.setFixedSize(self.w,self.h)
        self.pixmap = QPixmap(self.size())
        self.pixmap.fill(Qt.white)

        self.painter = QPainter()
        self.pen = QPen()
        self.penColor = QColor(0,0,0)
        self.brushColor = QColor(0,0,0)
        self.brush = QBrush()
        self.pen.setCapStyle(Qt.RoundCap)
        self.pen.setJoinStyle(Qt.RoundJoin)
        self.pen.setWidth(10)
        self.brush.setColor(self.brushColor)
        self.pen.setColor(self.penColor)

        #jda
        self.setMouseTracking(True)

    def paintEvent(self, event: QPaintEvent):
        """Override method from QWidget
        Paint the Pixmap into the widget
        """
        with QPainter(self) as painter:
            painter.drawPixmap(0, 0, self.pixmap)


    def mouseMoveEvent(self, event: QMouseEvent):
        """
        Override from QWidget
        Called when user moves and clicks on the mouse
        The widget only receives the events when the mouse 
        button is down unless mouseTracking == True.
        """
        QWidget.mouseMoveEvent(self, event)

    def mousePressEvent(self, event: QMouseEvent):
        self.drawRandomRect()

    def drawRandomRect(self):
        x0 = random.randrange(0,self.w-100)
        x1 = random.randrange(x0+10,self.w)
        y0 = random.randrange(0,self.h-100)
        y1 = random.randrange(y0+10,self.h)
        pw = random.randrange(1,20)

        r,g,b,a = [random.randrange(0,256) for i in range(4)]
        self.penColor = QColor(r,g,b)
        r,g,b,a = [random.randrange(0,256) for i in range(4)]
        self.brushColor = QColor(r,g,b,a)

        self.pen.setWidth(pw)
        self.brush.setColor(self.brushColor)
        self.pen.setColor(self.penColor)

        self.painter.begin(self.pixmap)
        self.painter.setRenderHints(QPainter.Antialiasing, True)
        self.painter.fillRect(QRect(QPoint(x0,y0),QPoint(x1,y1)),self.brushColor)
        self.painter.end()
        self.update()

    def mouseReleaseEvent(self, event: QMouseEvent):
        """Override method from QWidget"""
        QWidget.mouseReleaseEvent(self, event)


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        QMainWindow.__init__(self, parent)

        self.widget = MyWidget()
        self.setCentralWidget(self.widget)

if __name__ == "__main__":
    app = QApplication(sys.argv)

    w = MainWindow()
    w.show()
    sys.exit(app.exec())