【问题标题】:QML semicircle programming [duplicate]QML半圆编程[重复]
【发布时间】:2017-11-09 17:49:51
【问题描述】:
Rectangle {
    x: -185
    y: -92
    width: 214
    height: 184
    color: "red"
    border.color: "black"
    border.width: 5
    radius: 100
}

这段代码画了一个圆,但是我如何在 QML 中画一个半圆呢?

【问题讨论】:

    标签: qml


    【解决方案1】:

    您可以通过在此处放置一个遮蔽矩形 (z>0) 并将绘制的内容剪切到根窗口来遮盖这个“伪圆”的下部:

    import QtQuick 2.7
    import QtQuick.Window 2.2
    
    Window{
        id: root
        visible: true
        width: 300
        height: 300
    
        Item {
            width: 100
            height: 100
            anchors.centerIn: parent
            clip:true
    
            Rectangle{
                id: circ
                width: parent.width
                height: parent.height
                border.width: 2
                radius:1000
                border.color: "black"
            }
    
            Rectangle{
                id:mask
                width: parent.width
                height: parent.height/2
                anchors.bottom: parent.bottom
                z:4
            }
        }
    }
    

    更新: 或者不带面具的简化:

    import QtQuick 2.7
    import QtQuick.Window 2.2
    
    Window{
        id: root
        visible: true
        width: 300
        height: 300
    
        Item {
            id: semicirc
            width: 2*50
            height: 50
            anchors.centerIn: parent
            clip:true
    
            Rectangle{
                id: circ
                width: parent.width
                height: parent.width
                border.width: 2
                radius:1000
                border.color: "black"
            }
        }
    }
    

    【讨论】:

    • 您可以通过删除mask Rectangle 来简化它,将Item width 设置为50 (width / 2) 并在circ Rectangle 中执行height: width
    • 已更新,谢谢。
    【解决方案2】:

    事实上你的代码没有画圆,而是画圆角的矩形。如果您想要一些绘画功能,请使用Canvas(简单方法)或 QQuickItem(“正确”且更快的方法)。

    Canvas {
        width: 200
        height: 200
        onPaint: {
            var context = getContext("2d");
            context.arc(100,100,95,0,Math.PI);
            context.strokeStyle = "blue";
            context.lineWidth = 5;
            context.stroke();
        }
    }
    

    【讨论】:

    猜你喜欢
    • 2019-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    相关资源
    最近更新 更多