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. How I can clear canvas in QML?
Forum Updated to NodeBB v4.3 + New Features

How I can clear canvas in QML?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 2 Posters 11.0k Views 2 Watching
  • 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
    Koljasha
    wrote on last edited by
    #1

    Hello!
    Sorry for my English I use translate.google
    How I can clear canvas in QML?
    For exemle I draw line in canvas and when I click on mousearea canvas should be cleaned, but it's not happening. Console log write fine.
    This my exemple code:

    import QtQuick 2.7
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 640
        height: 480
        title: qsTr("Hello World")
    
        MouseArea {
            anchors.fill: parent
            onClicked: {
                console.log("Click...");
                canvas.clear_canvas();
            }
        }
    
    
        Canvas {
            id: canvas
            anchors.fill: parent
    
            onPaint: {
                var ctx = getContext("2d");
                ctx.lineWidth = 3;
                ctx.strokeStyle = "red";
    
                ctx.beginPath();
                ctx.moveTo(10, 10);
                ctx.lineTo(500, 300);
                ctx.stroke();
    
                ctx.lineWidth = 3;
                ctx.strokeStyle = "blue";
    
                ctx.beginPath();
                ctx.moveTo(500, 300);
    
                ctx.lineTo(50, 200);
                ctx.stroke();
            }
    
            function clear_canvas() {
                var ctx = getContext("2d");
                ctx.reset();
                canvas.requestPaint();
            }
        }
    }
    
    

    Thank you

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      AFAICS, you clear the canvas and right after you request a repaint which will trigger the onPaint function.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      K 2 Replies Last reply
      0
      • SGaistS SGaist

        Hi,

        AFAICS, you clear the canvas and right after you request a repaint which will trigger the onPaint function.

        K Offline
        K Offline
        Koljasha
        wrote on last edited by
        #3

        @SGaist I'm sorry, I don't understand you. clear_canvas function is called when I click on mousearea (console.log prints "Click"), but the Canvas is not cleared.
        I watched this lesson https://www.youtube.com/watch?v=CR2qQebqv6I, like almost all the same (if not to take a bunch of C ++).

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          AFAICS, you clear the canvas and right after you request a repaint which will trigger the onPaint function.

          K Offline
          K Offline
          Koljasha
          wrote on last edited by Koljasha
          #4

          @SGaist Thank you....
          I do not understand English, an translate and me even more confused ... my function clear() again is the fact that it is written in onPaint, redraws the lines .....
          I redid the test code a bit and it worked:

          import QtQuick 2.7
          import QtQuick.Window 2.2
          
          Window {
              visible: true
              width: 640
              height: 480
              title: qsTr("Hello World")
          
              Rectangle {
                  anchors.left: parent.left
                  anchors.bottom: parent.bottom
          
                  width: 70
                  height: 30
                  border.color: "black"
          
                  Text {
                      anchors.horizontalCenter: parent.horizontalCenter
                      anchors.verticalCenter: parent.verticalCenter
                      text: qsTr("Paint")
                      font.pointSize: 12
                  }
          
                  MouseArea {
                      anchors.fill: parent
                      onClicked: {
                          canvas.paint_canvas()
                          console.log("Paint...")
                      }
                  }
              }
          
              Rectangle {
                  anchors.right: parent.right
                  anchors.bottom: parent.bottom
          
                  width: 70
                  height: 30
                  border.color: "black"
          
                  Text {
                      anchors.horizontalCenter: parent.horizontalCenter
                      anchors.verticalCenter: parent.verticalCenter
                      text: qsTr("Clear")
                      font.pointSize: 12
                  }
          
                  MouseArea {
                      anchors.fill: parent
                      onClicked: {
                          canvas.clear_canvas()
                          console.log("Clear...")
                      }
                  }
              }
          
              Canvas {
                  id: canvas
                  anchors.fill: parent
          
                  function paint_canvas() {
                      var ctx = getContext("2d");
                      ctx.lineWidth = 3;
                      ctx.strokeStyle = "red";
          
                      ctx.beginPath();
                      ctx.moveTo(10, 10);
                      ctx.lineTo(500, 300);
                      ctx.stroke();
          
                      ctx.lineWidth = 3;
                      ctx.strokeStyle = "blue";
          
                      ctx.beginPath();
                      ctx.moveTo(500, 300);
          
                      ctx.lineTo(50, 200);
                      ctx.stroke();
                      canvas.requestPaint();
                  }
          
                  function clear_canvas() {
                      var ctx = getContext("2d");
                      ctx.reset();
          //            can be used
          //            ctx.clearRect(0,0,300,200);
                      canvas.requestPaint();
                  }
              }
          }
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Glad you found out and thanks for sharing !

            Since you have it working now please mark the thread as solved using the Topic Tools button so there other forum users may know a solution has been found :)

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

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