Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QML Opacity Inheritance

Tags:

opacity

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.

like image 798
aqua boy Avatar asked Feb 09 '12 01:02

aqua boy


People also ask

What is opacity in QML?

Opacity is specified as a number between 0.0 (fully transparent) and 1.0 (fully opaque). The default value is 1.0. When this property is set, the specified opacity is also applied individually to child items. This may have an unintended effect in some circumstances.

What is parent in QML?

All QML objects have an object parent, which is determined by the object hierarchy in which the object is declared.


2 Answers

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.

like image 108
pareshdbest Avatar answered Sep 19 '22 16:09

pareshdbest


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:

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.

Another possible solution would be using a shadereffect.

Thanks to peppe on #qt@freenode.

like image 38
ChALkeR Avatar answered Sep 21 '22 16:09

ChALkeR