Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Canvas drawing



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

    Canvas {
                id: mycanvas
                x:0
                y:40
                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)
                                    context.rect(ptrect["x"],ptrect["y"],L/2,H/2)
                                    var ind=listeind.get(i)
                                    context.text(ind,ptrect["x"]+L/2,ptrect["y"]+H/2)
                                 }
    

    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 anything.it should draw my rectangles and my text.so 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 https://doc.qt.io/qt-5/qml-qtqml-qt.html#rgba-method , 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 ```
    context.strokeStyle=
    ....
    context.rect(...
    context.text(

    context.stroke()

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

Log in to reply