如何在QML中实现带有内阴影的矩形?
请参阅以下链接中的示例:
更新:
这是我正在尝试做的事情的一个简化版本(它没有显示任何阴影):
import QtQuick 2.0
import QtGraphicalEffects 1.0
Item {
width: 400
height: 400
Item {
anchors.fill: parent
Rectangle {
id: myRectangle
anchors.centerIn: parent
width: 200
height: 200
color: "grey"
}
}
InnerShadow {
anchors.fill: myRectangle
cached: true
visible: true
horizontalOffset: 0
verticalOffset: 0
radius: 25
samples: 16
color: "#b0000000"
smooth: true
source: myRectangle
}
}对不起。我愚蠢的..。我在简化代码时弄错了(该项目用于DropShadow测试,可以正常工作)。它应该是这样的:
import QtQuick 2.0
import QtGraphicalEffects 1.0
Item {
width: 400
height: 400
Rectangle {
id: myRectangle
anchors.centerIn: parent
width: 200
height: 200
color: "grey"
}
InnerShadow {
anchors.fill: myRectangle
cached: true
visible: true
horizontalOffset: 0
verticalOffset: 0
radius: 25
samples: 16
color: "#b0000000"
smooth: true
source: myRectangle
}
}发布于 2014-12-16 23:00:55
我不知道为什么,但如果你在你试图投射阴影的项目上面使用项目,它就会起作用(在这种情况下,它恰好是根项目,但它不一定是):
import QtQuick 2.0
import QtGraphicalEffects 1.0
Item {
id: root
width: 300
height: 300
Rectangle {
id: myRectangle
anchors.centerIn: parent
width: 100
height: 100
color: "grey"
}
InnerShadow {
anchors.fill: root
cached: true
horizontalOffset: 0
verticalOffset: 0
radius: 16
samples: 32
color: "#b0000000"
smooth: true
source: root
}
}

我是在搜索相关的bug时从QTBUG-27213得到这个想法的。
发布于 2016-10-11 16:01:50
first comment中的https://bugreports.qt.io/browse/QTBUG-56467是这些问题的信息性答案:
(c) Gunnar Sletta:
InnerShadow将基于源对象的半透明区域在不透明区域中生成阴影。如果源是完全不透明的(就像此矩形的情况一样),结果将不会出现阴影。
DropShadow有一个transparentBorder属性,该属性默认情况下设置为true,以帮助用户获得预期的结果,即使是完全不透明的输入。InnerShadow缺乏这一点。
顺便说一句,InnerShadow还使用了“旧的”高斯编码,它需要更多的样本才能得到正确的结果
无论如何,在矩形上获得内部阴影的方法是用透明像素的单像素边框填充它。这也是为什么有时会将目标源项的父项作为效果源的原因。(请注意,这通常不起作用,而且父GPU实际上不支持作为源,因为当您执行此操作时,许多GPU将失败)。
下面的代码会产生预期的结果:
import QtQuick 2.4
import QtGraphicalEffects 1.0
Item
{
width: 320
height: 480
Item {
anchors.centerIn: parent
width: 100
height: 100
Rectangle {
anchors.fill: parent
anchors.margins: 1
color: "lightsteelblue"
}
layer.enabled: true
layer.effect: InnerShadow {
color: "black"
samples: 32
radius: 10
spread: 0.4
}
}
}或者,如果你想保存额外的项目,并且可以忍受一点小把戏:
Rectangle {
anchors.centerIn: parent
width: 100
height: 100
color: "lightsteelblue"
border.color: Qt.rgba(0, 0, 0, 0.01)
border.width: 1
layer.enabled: true
layer.effect: InnerShadow {
color: "black"
samples: 20
radius: 8.5
spread: 0.2
}
}( border.color不能是“透明的”,因为它被解释为没有边框)
https://stackoverflow.com/questions/27484767
复制相似问题