我不明白以下代码中不透明度的行为!
import QtQuick 2.4
Rectangle {
id: root
width: 640; height: 480
Rectangle {
id: wrapper
width: 600; height:440
anchors.centerIn: parent
color: "black"
opacity: 0.5
Rectangle {
id: belowcover
width: cover.width / 2
height: cover.height / 2
anchors.centerIn: parent
color: "green"
z: 1
}
Rectangle {
id: cover
width: root.width / 2
height: root.height / 2
anchors.centerIn: parent
color: "red"
z: 1
}
Rectangle {
id: seen
width: 100; height: 100
radius: width
color: "blue"
z: -1
}
}
}
wrapper
的不透明度为0.5,因此我可以透过它看到圆圈seen
。但是cover
和belowcover
都有1的不透明度,而且因为belowcover
小于cover
,所以不应该看到(或者说我希望它不会被看到,或者我错过了什么?)。但是看到了cover
和belowcover
。我想只通过wrapper
看到圆圈,而belowcover
将隐藏在cover
下方。我怎样才能做到这一点?我观察到将z
的cover
设置为高于belowcover
的opacity
并不会使后者隐藏。
编辑:我观察到当父级的不透明度设置为小于1时,子对象变得不那么不透明,即使它们的x
保持为1,如打印到控制台时所见。我不明白为什么。
当其父级的不透明度小于1时,不透明度为1但仍显示透明度的原因是:不透明度与其他属性(如y
,x
等)相同,因此,即使子项的不透明度为1,它与父母的相对。因此,如果父级具有不透明度0.5,并且子级的不透明度为1,则子级的绝对不透明度值实际为0.5。另一个不透明度为0.5的孩子实际上具有0.5x0.5 = 0.25的不透明度。这类似于孩子的x
值为0,当绝对several work-arounds可能不为0.这种设计在整个API中保持一致性。
有semi-transparent colors。我最喜欢的是为父母使用color
。尝试将wrapper
的color: "#88000000"
设置为belowcover
。你再也看不到绿色矩形cover
了。要看到这一点,你必须将cover
的不透明度设置为小于1的某个值,这意味着layer是不透明的。这就是我实际解决问题的方法。
但是,如果父项是图像,则无法应用此项。然后你必须采取其他一些技术。例如:
layer.enabled: true
。将changing the parent设为父母。