Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML Canvas Drawing Lines

QML Canvas Drawing Lines

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 18.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    krobinson
    wrote on 16 Dec 2015, 23:12 last edited by
    #1

    I am trying to draw some lines on a canvas. I need to be able to draw based on the pixels of the control they are in. Let me show the example and it will make more sense.

    Item
    {
    	id: mainContainer
    	height: 8
    	Rectangle
    	{
    		anchors.fill: parent
    		color: "white"
    	}
    	Canvas
    	{
    		id: drawingCanvas
    		anchors.fill: parent
    		onPaint:
    		{
    			var ctx = getContext("2d")
    			ctx.lineWidth = 15;
    			ctx.strokeStyle = "red"
    			ctx.beginPath()
    			ctx.moveTo(drawingCanvas.width / 2, 0)
    			ctx.lineTo((drawingCanvas.width / 2) + 10, 0)
    			ctx.closePath()
    			ctx.stroke()
    		}
    	}
    }
    

    What I would like this to do is draw a 10 pixel horizontal line starting at the halfway point of the main container. It doesn't function this way and I'm not sure what I'm doing wrong. It seems like maybe the canvas coordinates do relate to the pixels of the main container.

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on 17 Dec 2015, 04:00 last edited by A Former User
      #2

      Couple of issues there –

      • your Item has a height of 8 and no width specified
      • comment out closePath() - it doesn't do what you think it does

      http://bucephalus.org/text/CanvasHandbook/CanvasHandbook.html#closepath

      in an open path, this method call "closes" the path in the sense that it connects the current point to the starting point of the path. Note, that closePath() does not "finish" the path in the sense that it draws it. To do that, you still need to call the stroke() or fill() method.

      
          Item
          {
              id: mainContainer
              height: 100
              width: 100
              x: 200
              y: 200
      
              Canvas
              {
                  id: drawingCanvas
                  anchors.fill: parent
                  onPaint:
                  {
                      var ctx = getContext("2d")
      
                      ctx.fillStyle = "white"
                      ctx.fillRect(0,0,drawingCanvas.width ,drawingCanvas.height )
      
                      ctx.lineWidth = 15;
                      ctx.strokeStyle = "red"
                      ctx.beginPath()
                      ctx.moveTo(drawingCanvas.width / 2, 0)
                      ctx.lineTo((drawingCanvas.width / 2) + 10, 0)
                      //ctx.closePath()
                      ctx.stroke()
                  }
              }
          }
      
      1 Reply Last reply
      0

      1/2

      16 Dec 2015, 23:12

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved