我有一个带有圆角(半径)的简单矩形,但想为其背景色应用渐变。

Rectangle {
    id: rect
    width: 200
    height: 200

    radius: 20

    LinearGradient {
        anchors.fill: parent
        start: Qt.point(0,parent.height/2)
        end: Qt.point(parent.width,parent.height/2)
        gradient: Gradient {
            GradientStop { position: 0.0; color: "red" }
            GradientStop { position: 1.0; color: "green" }
        }
    }
}

我认为OpacityMask可能有帮助?我曾经尝试过一无所获。我想知道我是否只是缺少什么,或者解决方案是否更复杂。

最佳答案

好吧,不透明蒙版应该可以工作,但是您必须隐藏源以使圆角变得可见,否则它们将从背面显示...

  Rectangle {
    id: rect
    width: 200
    height: 200
    // radius: 20 - redundant
    visible: false // hide it!!!

    LinearGradient {
      anchors.fill: parent
      start: Qt.point(0,parent.height/2)
      end: Qt.point(parent.width,parent.height/2)
      gradient: Gradient {
        GradientStop { position: 0.0; color: "red" }
        GradientStop { position: 1.0; color: "green" }
      }
    }
  }

  OpacityMask {
    anchors.fill: rect
    source: rect
    maskSource: Rectangle {
      width: 200
      height: 200
      radius: 20
    }
  }

关于qt - QML-具有半径的矩形上的LinearGradient?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49075800/

10-11 22:46
查看更多