qt - 渐变忽略父圆形
问题描述
我正在尝试创建一个内部带有渐变颜色的圆圈。下面半径的矩形工作正常,但是一旦我添加了 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
}
}
}
解决方案
这应该做你想要的:
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
}
}
}
}
推荐阅读
- javascript - 如何通过控制器在网站 odoo 12 中选择下拉列表将数据保存到后端?
- python - 将项目添加到列表中的特定位置
- javascript - Javascript如何检查传入的参数是否是一个对象
- css - 更改 React Material UI 文本字段和大纲输入的边框颜色
- kubernetes - Is it possible to have multiple ingress resources with a single GKE ingress controller
- r - 用于在同一页面内绘制多个图的 R 程序代码
- android - Android 某些 mp4 文件未在 Whatsapp 上共享
- javascript - 数组作为 EJS 变量传递后变为字符串
- web-scraping - Python3 requests-html:不幸的是,自动访问此页面被拒绝
- java - 在所有 Spring Boot api 中添加强制标头,无需招摇