[Interest] QML Canvas: bug or programmer error?

Mark Summerfield list at qtrac.plus.com
Tue Feb 12 14:17:03 CET 2013


Hi Jens,

On Tue, 12 Feb 2013 11:59:59 +0000
Bache-Wiig Jens <Jens.Bache-Wiig at digia.com> wrote:
[snip]
> Hi Mark.
> 
> There is indeed a mistake in this example. You can think of Canvas as an
> Image Item that you paint on and the coordinates you specify in the
> paint function are local to that Image. Things that are painted outside
> of the Canvas rectangle will be clipped away and discarded.
> 
> In other words you should not use the x and y properties in the Canvas
> paint function itself since they will be used as the offset of the Image
> element resulting from the Canvas paint function. Each Triangle must be
> painted by itself with the origin starting at 0,0. To make it work you
> can simply redefine your paint function like this:
> 
> // Triangle.qml
> import QtQuick 2.0
> Canvas {
>    id: triangle
>    antialiasing: true
>    property color color: "white"
>    onPaint: {
>        var ctx = getContext("2d");
>        ctx.save();
>        ctx.fillStyle = color;
>        ctx.moveTo(width / 2, 0);
>        ctx.lineTo(width, height);
>        ctx.lineTo(0, height);
>        ctx.closePath();
>        ctx.fill();
>        ctx.restore();
>    }
> }
> 
> And it should work fine.

Yes, it does. Thanks for spotting my silly mistake:-)

-- 
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
    C++, Python, Qt, PyQt - training and consultancy
        "Programming in Go" - ISBN 0321774639
            http://www.qtrac.eu/gobook.html



More information about the Interest mailing list