QML 不透明度继承
在 QML 中,如何防止子元素继承其父元素的不透明度? 我想为父元素及其子元素设置不同的不透明度值。
In QML, how can I prevent a child element from inheriting the opacity from its parent?
I want to set different opacity values for the parent and it's child element.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
更多
发布评论
评论(9)
我认为,一种方法是使用半透明颜色,如此处 而不是不透明度。
例如,使用四色代码,例如
#800000FF
表示半透明蓝色。I think, one way would be to use semi transparent colors as described here instead of opacity.
e.g. using quad color code like
#800000FF
for semi transparent blue.实际上,为父元素设置
layer.enabled: true
对我来说就是这样。整个元素被渲染到缓冲区,并且
opacity
被应用到生成的缓冲区(一次到整个图层)。请参阅http://doc.qt.io/ qt-5/qml-qtquick-item.html#layer.enabled-prop
示例代码:
这是一个解决方案,但请确保您知道启用分层时在做什么。
另一种可能的解决方案是使用着色器效果。
感谢 #qt@freenode 上的 peppe。
Actually, setting
layer.enabled: true
for the parent element does the thing for me.The whole element is rendered to the buffer, and
opacity
is applied to the resulting buffer (to the whole layer at once).See http://doc.qt.io/qt-5/qml-qtquick-item.html#layer.enabled-prop
Example code:
That is a solution, but make sure that you know what you are doing when enabling layering.
Another possible solution would be using a shadereffect.
Thanks to peppe on #qt@freenode.
你不能。项目的不透明度值是相对于其父级的,因此如果您编写类似的代码,
您将看到两个矩形具有相同的不透明度。
You can't. Items opacity value is relative to their parents one, so if you code something like
You will see that the two rectangles have the same opacity.
我刚才遇到了这个问题。使用 Qt 5.1.0
在我的例子中,我有一个
矩形
元素,其opacity: 0.6
和一个子Image
元素。Image
继承了透明度 - 这不是我们想要的。为了解决这个问题,我将主
Rectangle
包含在Item
元素中。将大小/位置定义从Rectangle
传递到外部Item
。将Image
移至矩形
之外。最后,我将
Item
作为主要父级,并在Item
内并排放置Rectangle
和Image
。只有
Rectangle
保持不透明度 0.6,因此 Rectangle 具有透明度,而Image
完全不透明。I've bumped into this issue just now. Using Qt 5.1.0
In my case, I had a
Rectangle
Element withopacity: 0.6
and a childImage
element. TheImage
was inheriting the transparency - not desired.To solve it, I enclosed the main
Rectangle
in anItem
element. Passed the size/position definitions from theRectangle
to the outerItem
. Moved theImage
outside theRectangle
.In the end, I had
Item
as the main parent andRectangle
andImage
side by side, insideItem
.Only
Rectangle
maintained the opacity 0.6, so the Rectangle has transparency andImage
is fully opaque.这是可能的!您需要在 Component.onCompleted 范围内测试父级的不透明度。如果其值为 0,则需要将对象的父级更改为其当前父级的父级。
例子:
It's possible! You need to test in the
Component.onCompleted
scope the opacity of the parent. If its 0 you need to change the parent of your object to the parent of it's current parent.Example:
您无法阻止子元素从其父元素继承不透明度。
我个人的解决方法是将其更改为:
更改为:
或此(仅当父级为纯色时才可能):
You cannot prevent the child element from inheriting the opacity from its parent.
My personal work around is to change this:
Into this:
or this (only possible if the parent is a solid color):
我认为这是不可能的。您必须使两个元素成为兄弟元素并根据需要更改其不透明度。
I don't think its possible. you have to make two element sibling and changes its opacity as you wish.
不可能这样做,但您可以使用
Qt.lighter(color,opacity)
更改它们颜色
的
it is not possible to do that but you can change their color with
Qt.lighter(color,opacity)
for example
我在 Qt 4.8.6 中也遇到了这个问题。
在我的特定情况下,我希望顶级项目的透明度为 20%,黑色,但其子元素不受父元素的任何不透明度/透明度设置的影响。
由于 QML 的继承机制,不透明度不起作用。
但我能够使用 Qml Qt 对象中的 rgba 函数。这让我得到了我想要的,父元素现在是 20% 透明,但子元素不受影响。
注意:我也尝试过直接使用 RGBA 颜色代码,正如之前的海报所提到的,但它不起作用。
示例:
为任何其他 RGBA 值设置 alpha 值都有效,但纯黑色不起作用。
I also ran into this problem with Qt 4.8.6.
In my particular case, I wanted the top level item to be 20% transparent with black color, but have its child elements be unaffected by any opacity/transparency setting from the parent.
Opacity did not work, due to the inheritance mechanism of QML.
But I was able to use the rgba function from the Qml Qt object. This allowed me to get exactly what I wanted, the parent is now 20% transparent, but the child elements are unaffected.
Note: I also tried to use the RGBA color codes directly, as mentioned by a previous poster, but it did not work.
Example:
Setting the alpha value for any other RGBA values worked, just not with pure black.