具有圆角的图像,并在Qt中保留AspectFit

具有圆角的图像,并在Qt中保留AspectFit,qt,qml,Qt,Qml,我知道,但它不适用于: fillMode: PreserveAspectFit 这种变化会导致图片侧面出现纯色条带。有没有办法在没有它们的情况下工作?您可以使用图像的paintedWidth和paintedHeight属性(请参阅) 我发现使用QtGraphicalEffects中的OpacityMask比上述答案中的着色器更容易(不确定性能) 请注意,您可以使用项目而不是透明矩形 import QtQuick 2.11 import QtQuick.Window 2.11 import Qt

我知道,但它不适用于:

fillMode: PreserveAspectFit

这种变化会导致图片侧面出现纯色条带。有没有办法在没有它们的情况下工作?

您可以使用
图像的
paintedWidth
paintedHeight
属性(请参阅)

我发现使用QtGraphicalEffects中的
OpacityMask
比上述答案中的着色器更容易(不确定性能)


请注意,您可以使用项目而不是透明矩形
import QtQuick 2.11
import QtQuick.Window 2.11
import QtGraphicalEffects 1.0

Window {
    width: 640
    height: 480
    visible: true
    title: qsTr("Hello World")

    Image {
        id: img
        anchors.fill: parent
        source: "your_non_fitting_image.png"

        fillMode: Image.PreserveAspectFit
        visible: false
    }

    Item {
        id: mask
        anchors.fill: img
        visible: false

        Rectangle {
            color: "white"
            radius: 20
            anchors.centerIn: parent
            width: img.paintedWidth
            height: img.paintedHeight
        }
    }

    OpacityMask {
        anchors.fill: img
        source: img
        maskSource: mask
    }
}