【问题标题】:Gradient ignores parent rounded shape渐变忽略父圆形
【发布时间】:2021-05-04 01:08:18
【问题描述】:

我正在尝试创建一个内部带有渐变颜色的圆圈。下面半径的矩形工作正常,但是一旦我添加了 RadialGradient,它就会恢复为方形。我尝试添加不透明蒙版,但没有奏效。这有什么问题?

import QtQuick 2.0
import QtGraphicalEffects 1.0

Rectangle {
    id: light
    property string type: ""
    property bool connected: false
    property bool flagSet: false

    width: 50
    height: width
    radius: width / 2


    RadialGradient {
        anchors.fill: parent
        gradient: Gradient {
            GradientStop { position: 0.0; color: "green" }
            GradientStop { position: 0.5; color: "black" }
        }
    }


    OpacityMask {
        anchors.fill: parent
        source: light
        maskSource: Rectangle {
            height: light.height
            width: light.width
            radius: light.radius
        }
    }
}

【问题讨论】:

    标签: qt qml gradient rectangles


    【解决方案1】:

    这应该做你想做的:

        Rectangle {
            id: border
    
            width: light.width + 2
            height: width
            radius: width / 2
            color: "red"
    
            RadialGradient {
                id: light
                anchors.centerIn: parent
    
                width: 50
                height: width
    
                gradient: Gradient {
                    GradientStop { position: 0.0; color: "green" }
                    GradientStop { position: 0.5; color: "black" }
                }
    
                layer.enabled: true
                layer.effect: OpacityMask {
                    id: mask
                    maskSource: Rectangle {
                        height: light.height
                        width: light.width
                        radius: width / 2
                    }
                }
            }
        }
    

    【讨论】:

    • 效果很好 - 但是 - 我计划在圆圈周围添加 1 个像素边框(这就是我选择矩形的原因)。仅使用 RadialGradient 是否可行?
    • 我添加回父矩形作为边框。
    • 我想我现在明白了。我还尝试在渐变顶部分层一个带有边框的透明矩形 - 都可以
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-29
    • 2010-12-26
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多