QML画布笔触,在几个零件上具有不同的线条粗细

时间:2020-08-24 15:31:14

标签: qt canvas qml stroke

我创建了以下QML项目,该项目定义了自定义形状。我正在尝试以相同的线宽获得这种形状,而很少有比其他线段大的线段。

    Item {
    x: 100
    y: 100
    width: 400
    height:400
    Canvas {
        x: 10
        y: 10
        anchors.margins: 10
        readonly property int widthMultiplier: 11
        readonly property int heightMultiplier: 9
        readonly property int baseValue: 30
        readonly property int topCornerFirstMultiplier: 3
        readonly property int topCornerSecondMultiplier: 3
        readonly property int bottomCornerFirstMultiplier: 8
        readonly property int bottomCornerSecondMultiplier: 6
        anchors.fill:parent


        onPaint: {
            var context = getContext("2d");

            context.beginPath();
            context.moveTo(0, baseValue*topCornerFirstMultiplier);
            context.lineTo(0, baseValue*heightMultiplier);
            context.lineTo(baseValue*bottomCornerFirstMultiplier, baseValue*heightMultiplier);
            context.lineTo(baseValue*widthMultiplier, baseValue*bottomCornerSecondMultiplier);
            context.lineTo(baseValue*widthMultiplier, 0);
            context.lineTo(baseValue*topCornerSecondMultiplier, 0);
            context.lineTo(0, baseValue*topCornerFirstMultiplier);


            context.lineWidth = 3
            context.strokeStyle = "black"
            context.stroke();
        }
    }
}

我得到的形状如下: Shape

1 个答案:

答案 0 :(得分:1)

该线条似乎具有不同的宽度,因为它们是在画布区域之外绘制的(因此无法部分看到)。例如,

绘制的垂直线
            context.moveTo(0, baseValue*topCornerFirstMultiplier);
            context.lineTo(0, baseValue*heightMultiplier);

仅在画布的边缘绘制,因此其中一半不可见。要解决此问题,您可以手动添加一些填充。在您的情况下,我只是将0 x和y值替换为5,并按预期绘制了形状。

            context.moveTo(5, baseValue*topCornerFirstMultiplier);
            context.lineTo(5, baseValue*heightMultiplier);
            context.lineTo(baseValue*bottomCornerFirstMultiplier, baseValue*heightMultiplier);
            context.lineTo(baseValue*widthMultiplier, baseValue*bottomCornerSecondMultiplier);
            context.lineTo(baseValue*widthMultiplier, );
            context.lineTo(baseValue*topCornerSecondMultiplier, 5);
            context.lineTo(5, baseValue*topCornerFirstMultiplier);