Important: Please read the Qt Code of Conduct -

How to manualy repaint invisible area of qml canvas?

  • Hi! I have some code i my handler onPaint in qml Canvas. I need that this code run when app was minimized and then save canvas to the file by save() canvas method. requestPaint() don't work. this method don't emit paint for region because region is invisible, i think. have you any decision? Thank you!

  • Moderators

    @pnmrvvtl What do you mean by invisible area ?

  • @p3c0 my app may be run background or minimized. and area must be repainted and then saved to external file for example "1.jpg"
    i solve problem with background painting with signal paint(rect), but on method save() app get critical error...can anybody help me?Thanks...

  • Moderators

    @pnmrvvtl How are you calling save ? What is the exact error ? Can you post an example ?

  • @p3c0

    onGraphCreated: {
                canv.isGraphCreated = 1;
    /*     */

    canv is id of my canvas. graphcreated is my signal. this is text in qml/js console
    ASSERT: "rf.isValid()" in file items\context2d\qquickcontext2dtexture.cpp, line 660 global\qglobal.cpp: 2810

  • Moderators

    @pnmrvvtl Looking at the source here (Line 660), the assert occurs when the rect (viz. here the canvas) is not valid. But I tested something similar on Linux and it works. Following is the code:

    import QtQuick 2.4
    Canvas {
        id: root
        width: 200
        height: 200
        property point initialpos : Qt.point(0,0)
        property point finalpos: Qt.point(0,0)
        property int count : 0
        onPaint: {
            var ctx = getContext("2d")
            ctx.clearRect(0, 0, width, height);
            ctx.lineWidth = 2
            ctx.strokeStyle = "red"
        Timer {
            interval: 1000; running: true; repeat: true
            onTriggered: {
                initialpos = Qt.point(Math.floor(Math.random() * 200) + 1,Math.floor(Math.random() * 200) + 1)
                finalpos = Qt.point(Math.floor(Math.random() * 200) + 1,Math.floor(Math.random() * 200) + 1)

    This code just takes snaps of Canvas and save into directory after an interval of 1 sec. This works for me and there is no assert. Also root.requestPaint() instead of root.paint(0,0,root.width,root.height) works even if the Window is minimized.
    Can you test it on your system ?

Log in to reply