Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QT 5.7 QML quick semi-transparent rectangle, with rounded corners on one side

I want a semi-transparent rectangular shape using Qt Quick QML, but with rounded corners on 1 side only.

This is sort of the shape of the rectangle I want. If it were not see through, I would probably just overlap 2 rectangles, one with rounded corners and one without. However, this doesn't work with transparency, as the overlap becomes darker.

     ----------|
   /           |
 /             | 
|              |
|              |
|              |
 \             | 
   \           |   
     ----------|

Anybody have any ideas?

like image 667
James Avatar asked Nov 01 '16 04:11

James


1 Answers

You could use clipping (see performance docs) to cut off the corners of a single rounded rectangle:

import QtQuick 2.5
import QtQuick.Window 2.2

Window {
    width: 300
    height: 300
    visible: true

    Item {
        width: 100
        height: 100
        anchors.centerIn: parent
        clip: true

        Rectangle {
            anchors.fill: parent
            anchors.rightMargin: -radius
            radius: 10
            color: "navajowhite"
            opacity: 0.5
        }
    }
}

You could also use layers to avoid the overlapping transparency issue:

import QtQuick 2.5
import QtQuick.Window 2.2

Window {
    width: 300
    height: 300
    visible: true

    Item {
        width: 100
        height: 100
        opacity: 0.5
        layer.enabled: true
        anchors.centerIn: parent

        Rectangle {
            color: "navajowhite"
            radius: 10
            anchors.fill: parent
        }
        Rectangle {
            color: "navajowhite"
            anchors.fill: parent
            anchors.leftMargin: 10
        }
    }
}

As mentioned by @folibis, you can also use Canvas, for which there is already a similar answer.

like image 125
Mitch Avatar answered Sep 21 '22 14:09

Mitch