在qml中,要实现绘图,就必须要用到下面的东西:
Canvas: 画布
context: 画师
Pen: 画笔
Brush: 画刷
坐标系: qml中,画布的原点是在左上角的。
在qml中要设置上面的属性需要通过一下的代码来实现:
在onPaint信号中
{
var ctx = getContext(“2d”);
ctx.lineWidth = 2;
ctx.strokeStyle = “red”;
ctx.fillStyle = “blue”;
ctx.beginPath();
ctx.rect(100 ,100 ,50 ,50);
ctx.fill();
ctx.stroke();
}
这样就可以实现绘制一个简单的图形了。
下面就以绘制一个三角形来作为例子来学习:
import QtQuick 2.2
Canvas{
width: 400;
height: 240;
contextType: "2d";
onPaint: {
context.lineWidth = 2;
context