我想要一个比默认垂直渐变更具体的渐变Rectangle。我尝试添加 aLinearGradient以实现对角线效果,但它会覆盖边框。
考虑这个例子。顶部Rectangle有垂直渐变和边框。底部Rectangle渐变覆盖边框和radius。我尝试过clip,gradient: LinearGradient但它们也不起作用。
import QtQuick 2.7
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtGraphicalEffects 1.0
ApplicationWindow
{
visible: true
width: 640
height: 480
Column
{
spacing: 20
width: parent.width
Rectangle
{
width: 200
height: 200
border.width: 4
border.color: "#888"
radius: 10
// adds a vertical gradient to button
gradient: Gradient
{
GradientStop
{
position: 0
color: "#eee"
}
GradientStop
{
position: 1
color: "#ccc"
}
}
}
Rectangle
{
width: 200
height: 200
border.width: 4
border.color: "#888"
radius: 10
// try to add diagonal gradient, but overwrites button border
// also can't do, gradient: LinearGradient ?
LinearGradient
{
anchors.fill: parent
start: Qt.point(0,0)
end: Qt.point(parent.width,parent.height)
gradient: Gradient
{
GradientStop
{
position: 0
color: "#eee"
}
GradientStop
{
position: 1
color: "#ccc"
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果如下:
我可以理解为什么这可能会产生这样的结果,但是如何将渐变剪辑为Rectangle带有 a 的radius?
clip始终在正在剪切的边界矩形处进行剪切Item,并且不关心alpha-values。
然而LinearGradient,还有另一个工具可以实现您想要的:
- source-property。
看这个例子:
import QtQuick 2.0
import QtQuick.Window 2.0
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtGraphicalEffects 1.0
Window {
width: 1024
height: 800
visible: true
Rectangle {
id: rect1
width: 100
height: 100
radius: 20
}
LinearGradient {
anchors.fill: rect1
source: rect1 <-- Here is where you specify its shape.
start: Qt.point(0, 0)
end: Qt.point(300, 300)
gradient: Gradient {
GradientStop { position: 0.0; color: "white" }
GradientStop { position: 1.0; color: "black" }
}
}
}
Run Code Online (Sandbox Code Playgroud)