2

I am trying to make the first image, look like the second image

unclipped rectangle

clipped rectangle

The goal to is to clip the green child rect to match the inside radius and edge bounds of the parent rect. Unfortunately, the clip property on a QML Rectangle only clips children to the bounding rect of the parent object, meaning radius is excluded in the clipping.

Most of the solutions suggested online use either a OpacityMask (Qt5) or a MultiEffect (Qt6) QML object to achieve said effect. I would prefer to stick with Qt6 native objects instead of deprecated Qt5 versions and I have tried many variations of using MutiEffect with none working.

In a basic example, I'm looking at something like this:

Rectangle
{
    id: parentRect
    width: 300
    height: 300
    anchors.centerIn: parent
    color: "blue"
    radius: 50

    Rectangle
    {
        id: childRect
        color: "green"
        anchors.bottom: parent.bottom
        height: parent.height * 0.1
        width: parent.width
    }
}

MultiEffect
{
    source: childRect
    anchors.fill: childRect
    maskEnabled: true
    maskSource: parentRect                    
}

The desired result is that the childRect is clipped to the radius of the parentRect making the illusion that the rect is "running off" the edge of the parentRect without actually spilling over.

I have tried many other variations of the above code snippet with no success (including other answers on SO to related questions). I don't want to include all of that code in this question, but I would be happy to provide more details on what else I've tried in the comments if needed.

Can this effect be achieved using QML and MultiEffect? Or do I have to use some other solution for Qt6? I would find it hard to believe if something this (seemingly) simple could not be done in QML.

1 Answer 1

2

You're nearly there. To get the best out of MultiEffect:

    1. Recommended that source and maskSource should be distinctly separate (i.e. one should not be a child of the other)
    1. source and maskSource should be the same dimensions
    1. source should set visible: false
    1. the maskSource should set layer.enabled to true

Putting it all together:

    Rectangle
    {
        id: parentRect
        width: 300
        height: 300
        anchors.centerIn: parent
        color: "blue"
        radius: 50
        layer.enabled: true
    }
    Item {
        id: childRect
        anchors.fill: parentRect
        visible: false
        Rectangle
        {
            color: "green"
            anchors.bottom: parent.bottom
            height: parent.height * 0.1
            width: parent.width
        }
    }
    MultiEffect
    {
        anchors.fill: parentRect
        maskEnabled: true
        source: childRect
        maskSource: parentRect
    }

https://i.sstatic.net/JREWRM2C.png

Sign up to request clarification or add additional context in comments.

1 Comment

Bingo, that worked! Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.