[Interest] [QML] Different behaviour between QML Scene and QQmlApplicationEngine when using getContext("2d")

Christoph Petzold christoph.petzold at hasomed.de
Thu Jan 29 14:49:00 CET 2015


Hello,

i do not understand the behaviour of my compiled Qt-Quick-C++ application when using 

property var ctx: getContext("2d")

in a simple qml Canvas component. I do get run time errors

qrc:/NotWorkingRectangle.qml:7:5: QML Canvas: Unable to use getContext() at this time, please wait for available: true
qrc:/NotWorkingRectangle.qml:16: TypeError: Type error

and nothing will be painted. If i use the same qml files with QML Scene tool everything is painted as expected and no run time errors are shown. Do you have any advice why? Minimal samples follow:

main.cpp:
-------------
#include <QApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    return app.exec();
}

main.qml:
--------------
import QtQuick 2.3
import QtQuick.Controls 1.2

ApplicationWindow {
    visible: true
    width: 640
    height: 480

    NotWorkingRectangle {
    }
}

NotWorkingRectangle.qml:
-----------------------------------
import QtQuick 2.3
import QtQuick.Controls 1.2
Rectangle {
    anchors.fill: parent
    Canvas {
        property var ctx: getContext("2d")
        anchors.fill: parent
        onWidthChanged: { requestPaint() }
        onPaint: { myPaint() }

        function myPaint() {
            ctx.strokeStyle = "black"
            ctx.shadowBlur = 0
            ctx.beginPath()
            ctx.moveTo(0, 0)
            ctx.lineTo(width/2, height/2)
            ctx.stroke()
            ctx.closePath()
        }
    }
}

In my real world component i have lots of painting functions and i do not want to pass the ctx element every time i want to use it. I also do not want to call getContext("2d") locally. I know it returns the same Object every next call but it is just code i would like to spare.

Mit freundlichen Grüßen/ Best regards

Christoph Petzold
Mitarbeiter Abteilung Funktionelle Elektrostimulation



HASOMED GmbH
Paul-Ecke-Str. 1
39114 Magdeburg

Tel.: +49 391  62 30 112
Fax: +49 391  62 30 113
E Mail: christoph.petzold at hasomed.de
Web: www.hasomed.de

HRB 108418
UST IdNo DE 114 161 708
Steuernummer 102/111/09705
GF Dr. Peter Weber, Frank Schulze





More information about the Interest mailing list