如何在QML中绘制图像的梯形形状?

时间:2020-07-03 18:29:02

标签: qt canvas qml polygon

我想在qml中绘制一个梯形,并将梯形图像内部的图像作为裁剪的图像。

1 个答案:

答案 0 :(得分:0)

我能够通过Canvas解决它,这是我的解决方案,感谢大家调查我的问题;

帆布{ id:帆布 anchors.fill:父级

    property string imagefile:"qrc:/myImage.png"
    onPaint: {
        var ctx = canvas.getContext('2d');
        ctx.beginPath();
        ctx.moveTo(757,0);
        ctx.lineTo(1163,0);
        ctx.lineTo(1486, 1080);
        ctx.lineTo(434, 1080);
        ctx.lineTo(757,0);
        ctx.closePath()
        ctx.clip();

    }

    onImageLoaded:{
        var ctx = canvas.getContext("2d");
        ctx.scale(1/Screen.devicePixelRatio,1/Screen.devicePixelRatio);
        ctx.drawImage(canvas.imagefile,0,0,1920,1080);
        ctx.scale(Screen.devicePixelRatio,Screen.devicePixelRatio);
        canvas.requestPaint();
    }

    Component.onCompleted: {

        loadImage(canvas.imagefile);
    }
}