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. Draw a line between two mouse clicks.
QtWS25 Last Chance

Draw a line between two mouse clicks.

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qml canvasmouse eventsqml
1 Posts 1 Posters 618 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.
  • I Offline
    I Offline
    Itdoesntwork
    wrote on 11 Oct 2020, 20:39 last edited by
    #1

    I am trying to make something like a small drawing app and I need to make it draw a line between two mouse clicks. I'm new to qml so I used some code from other websites but my idea was like this:

    import QtQuick 2.0
    
    Rectangle {
        width: 720
        height: 720
    
        property int xpos
        property int ypos
        property int prevxpos
        property int prevypos
        Canvas {
            id: myCanvas
            anchors.rightMargin: 0
            anchors.bottomMargin: 0
            anchors.leftMargin: 0
            anchors.topMargin: 0
            anchors.fill: parent
            renderTarget: Canvas.Image
            renderStrategy: Canvas.Immediate
            onPaint: {
                var ctx = getContext('2d')
                ctx.fillStyle = "blue"
                ctx.fillRect(xpos-3, ypos-1, 5, 5)
                ctx.lineWidth = 7;
                                ctx.strokeStyle = "red"
                                ctx.beginPath()
                                ctx.moveTo(prevxpos, prevypos)
                                ctx.lineTo(xpos, ypos)
    
                                ctx.stroke()
    
            }
    
            MouseArea{
                anchors.fill: parent
                onPressed: {
                    prevxpos = ypos
                    prevypos = xpos
                    xpos = mouseX
                    ypos = mouseY
                    myCanvas.requestPaint()
                }
                onMouseXChanged: {
                    var prevxpos = xpos
                    var prevypos = xpos
                    xpos = mouseX
                    ypos = mouseY
                    myCanvas.requestPaint()
                }
                onMouseYChanged: {
                    var prevxpos = xpos
                    var prevypos = ypos
                    xpos = mouseX
                    ypos = mouseY
                    myCanvas.requestPaint()
                }
    }
    
    

    prevxpos recieves the value of xpos before xpos gets updated and then onPaint darws a line between the two points. However, the line just doesn't appear. What am I doing wrong?

    1 Reply Last reply
    0

    1/1

    11 Oct 2020, 20:39

    • Login

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