The look and feel I'm trying to go for is to have a solid color button, and text on it like "Hello World" where the text is completely transparent, and the background shows through the button.
In other words, having text as a transparency mask on a button element.
I think this is what OpacityMask is for.
The sample below seems to illustrate the effect you're looking for. The background is a solid red
Rectangle
. The foreground is a solid blueRectangle
. AText
object, which black text is on top of the blue foreground. TheOpacityMask
uses theText
as a mask for the background, which results in red text appearing.Update: Added a more complex background to better illustrate that the
OpacityMask
is really computing cut through layer. Also added an animation to show the computed cut through changing over time.What you want is an
OpacityMask
that could be inverted. This is planned in a future release of Qt (5.7.1 maybe ?) and in fact you can already take a look at it : https://github.com/qt/qtgraphicaleffects/blob/5.7.1/src/effects/OpacityMask.qmlMeanwhile you could copy the code in a file named
MyOpacityMask.qml
or something else and use it this way with a Button from Qt Quick Controls 2 :This has previously been mentioned in Opposite for OpacityMask
Here is one way to do it:
Using it:
Result:
The button is red, the text is grey from the grey background, and it will accurately show anything that's beneath the button.
Obviously, the button is rudimentary, but the example outta be enough to get you going and implement something according to your needs.
The key element here is the custom shader, which is a very basic one - it colorizes every fragment and applies the mask as alpha. Obviously, you can use
ShaderEffectSource
to turn any QML Item to a texture, and replace theShaderEffectSource
with anothersampler 2D
and mix the two textures in any way you want, cut using the alpha channel, or any of the RGB if you are using a grayscale mask. And unlike the rather limitedOpacityMask
element, this will actually cut through and show anything that is underneath as it is supposed to.You can achieve that using
layer
attached property as follow without usingOpacityMask
.Also you does not any limitation and you can use any qml item, use any
QtQuick.Controls
and style it as usual :)