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

How to draw dashed line in Canvas



  • This code draws a solid line:

    Canvas {
        id: canvas
        onPaint: {
            var ctx = canvas.getContext('2d')
    
            ctx.strokeStyle = "red"
            ctx.lineWidth = 4
            ctx.lineCap = "round"
            ctx.lineJoin = "round"
            ctx.setLineDash = [ 1, 4 ]
    
            ctx.beginPath()
            ctx.moveTo(2, canvas.height / 2)
            ctx.lineTo(canvas.width - 2, canvas.height / 2)
            ctx.stroke()
        }
    }
    

    what I further have to set after setLineDash?


  • Moderators

    @Mark81

    ctx.setLineDash = [ 1, 4 ]

    AFAIK setlineDash is a function ->

    ctx.setLineDash( [ 1, 4 ] )
    


  • Ups, you're right. I didn't notice that because I received no warnings/error at compile-time nor at run-time!


Log in to reply