QT 5.7 QML快速半透明矩形,一侧有圆角

QT 5.7 QML quick semi-transparent rectangle, with rounded corners on one side

我想要一个使用 Qt Quick QML 的半透明矩形,但只有一侧有圆角。

这就是我想要的矩形形状。如果它不透明,我可能只会重叠 2 个矩形,一个有圆角,一个没有。但是,这不适用于透明度,因为重叠会变得更暗。

     ----------|
   /           |
 /             | 
|              |
|              |
|              |
 \             | 
   \           |   
     ----------|

有人有什么想法吗?

您可以使用 clipping (see performance docs) 来切掉单个圆角矩形的角:

import QtQuick 2.5
import QtQuick.Window 2.2

Window {
    width: 300
    height: 300
    visible: true

    Item {
        width: 100
        height: 100
        anchors.centerIn: parent
        clip: true

        Rectangle {
            anchors.fill: parent
            anchors.rightMargin: -radius
            radius: 10
            color: "navajowhite"
            opacity: 0.5
        }
    }
}

您也可以使用 layers 来避免重叠透明度问题:

import QtQuick 2.5
import QtQuick.Window 2.2

Window {
    width: 300
    height: 300
    visible: true

    Item {
        width: 100
        height: 100
        opacity: 0.5
        layer.enabled: true
        anchors.centerIn: parent

        Rectangle {
            color: "navajowhite"
            radius: 10
            anchors.fill: parent
        }
        Rectangle {
            color: "navajowhite"
            anchors.fill: parent
            anchors.leftMargin: 10
        }
    }
}

如@folibis所述,您还可以使用Canvas, for which there is already a similar answer