Qml canvas 2d没有要显示的行

时间:2016-03-04 12:10:37

标签: canvas qml

以下是我试图在画布上绘制十字线的代码片段

function drawAxis(context)
{
    context.lineWidth = 1;
    context.strokeStyle = Qt.rgba(0, 0, 0, 1);

    context.beginPath();

    // draw axis
    context.moveTo(0, center_h);
    context.lineTo(width, center_h);
    context.moveTo(center_w, 0);
    context.lineTo(center_w, height);
    context.closePath();
}

但实际上并没有显示任何内容。这可能是个问题?

1 个答案:

答案 0 :(得分:2)

事实证明,如果未调用stroke()函数,则不会绘制线条。因此,最终的工作代码如下:

function drawAxis(context) {
    context.lineWidth = 1
    context.strokeStyle = Qt.rgba(0, 0, 0, 1)
    context.beginPath()
    context.moveTo(0, center_h)
    context.lineTo(width, center_h)
    context.moveTo(center_w, 0)
    context.lineTo(center_w, height)
    context.closePath()
    context.stroke()                      // <--- lines are drawn here!
}