1
Rectangle {
    x: -185
    y: -92
    width: 214
    height: 184
    color: "red"
    border.color: "black"
    border.width: 5
    radius: 100
}

This code draws a circle, but how can I draw a semicircle in QML?

2 Answers2

2

You could mask the lower part of this "pseudocircle" by placing a masking rectangle there (z>0) and clipping the painted content to the root window:

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
        }
    }
}

Update: Or simplified without a mask:

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"
        }
    }
}
FloHe
  • 313
  • 1
  • 3
  • 10
0

In fact your code doesn't draw circle, that draws rectangle with round corners. If you want some painting functionality use Canvas (easy way) or QQuickItem ("right" and faster way).

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();
    }
}
folibis
  • 12,048
  • 6
  • 54
  • 97
  • 1
    Or if OP can use Qt 5.10, the new [Shape module](https://doc-snapshots.qt.io/qt5-5.10/qml-qtquick-shapes-shape.html) – GrecKo Nov 09 '17 at 09:12