I'm working on a project in Qt6, utilizing Qt Design Studio.
I'm currently facing an issue related to a Canvas
element in QML. Specifically, I'm trying to use the onPaint
signal handler, but I encounter the following error:
Cannot assign to non-existent property "onPaint"
I've already ensured that I'm importing both QtQuick 2.15
and QtQuick.Controls 2.15
.
Below is the relevant code snippet:
import QtQuick 2.15
import QtQuick.Controls 2.15
...
Canvas {
id: canvas
anchors.fill: parent
onPaint: {
// Some code...
}
}
Can anyone help me understand why I'm getting this error and how I can resolve it?
Additional info:
EDIT 1:
As suggested, I have removed the version numbers from the imports, but the error still occurs.
New code:
import QtQuick
import QtQuick.Controls
Canvas {
onPaint: {
// Some code...
The Qt6.x documentation https://doc.qt.io/qt-6/qml-qtquick-canvas.html actually has a mistake. It should not include a version number in the import line imports:
import QtQuick
Canvas {
id: mycanvas
width: 100
height: 200
onPaint: {
var ctx = getContext("2d");
ctx.fillStyle = Qt.rgba(1, 0, 0, 1);
ctx.fillRect(0, 0, width, height);
}
}
You can Try it Online!