Canvas drawing

  • Hi, I try to draw in a Canvas but it doesn't draw anything :

    Canvas {
                id: mycanvas
                width:  mainwindows.width
                height: mainwindows.height-40
                ListModel {
                    id: listerect
                ListModel {
                    id: listeind
    onPaint: {
                            var context = mycanvas.getContext("2d");
    			//init L,H                
                            context.clearRect(0, 0, mycanvas.width, mycanvas.height);
                            context.fillStyle = Qt.rgba(0, 255, 0, 0)
                            context.font = "bold 8px sans-serif";
                                for(var i=0; i < listerect.count; i++){
                                    var ptrect = listerect.get(i)
                                    var ind=listeind.get(i)

    I have some listerect.append({"x": i, "y": j)}) in a loop in another function
    has someone a tip ? I call mycanvas.requestPaint() in a further function (when I click on a button)

  • Hi, you need to use mycanvas.requestPaint() on some events, for example onWidthChanged: mycanvas.requestPaint()

  • Hi @SergeiLitovko yes but now,I use it (mycanvas.requestPaint())at the end of a function that defines and put the point in the listmodel (that is called in onclicked of a button) and it doesn't draw should draw my rectangles and my there is something wrong with the canvas.
    it is a program that I have modified to put a canvas,because I wasn't able to delete my window (and the objects )each time I click on the button to create my objects

  • You are drawing with fully transparent color (alpha = 0). Furthermore, according to , the values should be 0-1, so you should set context.fillStyle to Qt.rgba(0, 1, 0, 1).

  • @Astrinus thank you, I corrected that and the point ptrec but it doesn't still draw anything

  • ok it is ```


    so my problem is solved,I should have used strokeStyle and stroke()
    Best regards

Log in to reply