Canvas Qml画布2d不显示任何行

Canvas Qml画布2d不显示任何行,canvas,qml,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.

这是一段代码,我试图在画布上画一条交叉线

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();
}

但事实上什么也没有展示出来。这里可能有什么问题?

如果不调用该函数,则不会绘制线条。因此,最终工作代码如下:

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!
}
函数drawAxis(上下文){
context.lineWidth=1
context.strokeStyle=Qt.rgba(0,0,0,1)
context.beginPath()
上下文。移动到(0,居中)
context.lineTo(宽度、中心高度)
context.moveTo(中心w,0)
上下文.lineTo(中心,高度)
context.closePath()

context.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!
}
函数drawAxis(上下文){
context.lineWidth=1
context.strokeStyle=Qt.rgba(0,0,0,1)
context.beginPath()
上下文。移动到(0,居中)
context.lineTo(宽度、中心高度)
context.moveTo(中心w,0)
上下文.lineTo(中心,高度)
context.closePath()

context.stroke()//这是多么有趣的一个小时。谢谢。这是多么有趣的一个小时。谢谢。