本文介绍了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.

推荐答案

实际上,为父元素设置layer.enabled: true对我有用.整个元素都呈现到缓冲区,而opacity应用于生成的缓冲区(一次应用于整个层).

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).

请参见 http://doc.qt .io/qt-5/qml-qtquick-item.html#layer.enabled-prop

示例代码:

Rectangle {
    width: 400
    height: 200
    opacity: 0.5
    layer.enabled: true
    Rectangle {
            width: parent.width
            height: parent.height
            color: 'red'
    }
    Rectangle {
            width: parent.width / 2
            height: parent.height
            color: 'blue'
    }
}

这是一个解决方案,但是请确保启用分层时您知道自己在做什么.

That is a solution, but make sure that you know what you are doing when enabling layering.

另一种可能的解决方案是使用shadereffect.

Another possible solution would be using a shadereffect.

感谢在#qt @ freenode上peppe.

Thanks to peppe on #qt@freenode.

这篇关于QML不透明度继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 16:19