0

I have a QQuickPaintedItem in PyQt5, but the "paint" method is never being called.

I see this related question: Paint method of QQuickPaintedItem not called

In that question, the reason that paint is never being called is because the custom object didn't have a defined width/height. That's not the issue in my case, and yet the QQuickPaintedItem is still never calling "Paint". Here is a minimal example:

import sys

from PyQt5.QtCore import Qt, QCoreApplication
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQml import QQmlApplicationEngine, qmlRegisterType
from PyQt5.QtGui import QPainter, QColor, QBrush
from PyQt5.QtQuick import QQuickPaintedItem


class Board(QQuickPaintedItem):
    def __init__(self, parent):
        super().__init__()

    def paint(self, painter: QPainter):
        painter.setBrush(QBrush(QColor("red")))
        painter.drawRect(0, 0, 50, 50,)

        print("I'm being drawn")

    def componentComplete(self) -> None:
        print(f"Done! My size is {self.width()}, {self.height()}, and my coords are {self.x()}, {self.y()}")
        print(self.isVisible())

        self.update()


if __name__ == '__main__':
    QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
    QCoreApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)

    app = QApplication(sys.argv)
    engine = QQmlApplicationEngine()

    ctx = engine.rootContext()
    qmlRegisterType(Board, 'Board', 1, 0, 'Board')
    engine.load("app.qml")

    app.exec()

App.qml

import QtQuick 2.6
import QtQuick.Controls 2.15

import Board 1.0

ApplicationWindow {
    id: window

    width: 800
    height: 900

    visible: true

    Board {
        id: board

        anchors.centerIn: parent
        x: 0
        y: 0
        width: 700
        height: 700
    }
}

Also using a timer to change the width/height dynamically does not make it redraw either, and the "update" method which is being called in "componentCompleted" should trigger a redraw, right?

DJMcMayhem
  • 7,285
  • 4
  • 41
  • 61

0 Answers0