Image rounded corners in QML

前端 未结 7 1089
遇见更好的自我
遇见更好的自我 2020-12-14 06:25

To my surprise, the Image component has no radius property. I tried emulating the rounded corners by putting the image in a rounded Rectangle

7条回答
  •  佛祖请我去吃肉
    2020-12-14 06:37

    While both the accepted answer and the one from @fury worked equally well for me (Qt 5.9.3), they both left some aberrations in the corners when applied to raster images (didn't have those with SVG). What worked best for me in all cases was to apply the OpacityMask to a surrounding item, e.g. like the rectangle in the original post.

    Rectangle {
        id: root;
        anchors.right: rectContentBg.left
        anchors.top: rectContentBg.top
        anchors.margins: 8
    
        radius: 8
    
        width: 64
        height: 64
    
        // apply rounded corners mask
        layer.enabled: true
        layer.effect: OpacityMask {
            maskSource: Rectangle {
                x: root.x; y: root.y
                width: root.width
                height: root.height
                radius: root.radius
            }
        }
    
        Image {
            id: imgAuthor
            opacity: 1
            smooth: false
            anchors.fill: parent
            source: "qrc:/res/sample_avatar.jpg"
        }
    }
    

提交回复
热议问题