Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt 渐变忽略父圆角形状_Qt_Qml_Gradient_Rectangles - Fatal编程技术网

Qt 渐变忽略父圆角形状

Qt 渐变忽略父圆角形状,qt,qml,gradient,rectangles,Qt,Qml,Gradient,Rectangles,我试图创建一个内部带有渐变色的圆。下面带半径的矩形效果很好,但我一加上半径渐变,它就会恢复为方形。我试着添加一个不透明度遮罩,但没有效果。这个怎么了 import QtQuick 2.0 import QtGraphicalEffects 1.0 Rectangle { id: light property string type: "" property bool connected: false property bool flagSet

我试图创建一个内部带有渐变色的圆。下面带半径的矩形效果很好,但我一加上半径渐变,它就会恢复为方形。我试着添加一个不透明度遮罩,但没有效果。这个怎么了

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

这应该满足您的要求:

    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个像素的边框(这就是为什么我选择了矩形)。这可能仅仅是径向梯度吗?我在父矩形中添加了一个边框。我想我现在明白了。我还尝试过在渐变的顶部分层一个带边框的透明矩形,两种方法都可以