6

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

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

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

有人有什么想法吗?

4

1 回答 1

10

您可以使用剪裁(请参阅性能文档)来切掉单个圆角矩形的角:

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

您还可以使用图层来避免重叠透明度问题:

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,已经有一个类似的答案

于 2016-11-01T08:46:06.773 回答