How to paint on qml canvas on signal clicked from qt quick contols button?
I have example where painting on canvas in qml starts at beggining of application:
@import QtQuick 2.0
// canvas size
width: 200; height: 200
// handler to override for drawing
// get context to draw with
var ctx = getContext("2d")
// setup the stroke
ctx.lineWidth = 4
ctx.strokeStyle = "blue"
// setup the fill
ctx.fillStyle = "steelblue"
// begin a new path to draw
// top-left start point
// upper line
// right line
// bottom line
// left line through path closing
// fill using fill style
// stroke using line width and stroke style
How to run this code for painting when i click at button created in qml. It seems to be like how to connect qml signal from button and slot in qlm for painting some figure without using c++. Thank you.
Signal/slot connection in QML works like this:
mybutton.onClicked.connect( root.requestPaint )
Or you could simply write:
is "root" object created by me? and requestPaint() is slot created by me?
"root" is the id of the canvas in your example and requestPaint() is a slot of the Canvas component. Take a look at the "Canvas documentation":http://doc.qt.io/qt-5/qml-qtquick-canvas.html to see what requestPaint() does.