【问题标题】:QML - Opacity of stacked elementsQML - 堆叠元素的不透明度
【发布时间】:2016-10-12 07:43:01
【问题描述】:

我有两个堆叠的物品。这两个项目都有一个半透明的背景。圆圈现在显示下面的圆角矩形。

有什么方法可以隐藏与圆圈重叠的长圆角矩形部分?也许改变父级,圆的背景是从更高的祖先那里拉出来的,因此忽略它下面的矩形?

代码如下:

Item
{
    id: choice1
    width: 300
    height: 100

    Rectangle
    {
        id: choiceLabel1
        width: 0
        height: parent.height / 1.5
        radius: parent.height * 0.5
        color: "#88808080"
        anchors
        {
            verticalCenter: choice1.verticalCenter
            left: choice1.left
            leftMargin: choiceIcon1.width / 2
        }
        border
        {
            width: 2
            color: "red"
        }
    }
    Rectangle
    {
        id: choiceIcon1
        width: choice1.height
        height: choice1.height
        radius: width * 0.5
        color: "#88808080"
        anchors
        {
            verticalCenter: choice1.verticalCenter
            left: choice1.left
        }
        border
        {
            width: 2
            color: "red"
        }
    }
}

【问题讨论】:

  • 这样使用不让用户看到背后是什么 opacity的最终目的是什么?
  • 长矩形不显示,并且您的代码由于缺少类型而无法运行。
  • QML Opacity Inheritance的可能重复
  • 不,不是同一个问题。这里的不透明度也不是继承的。我想控制哪些元素在不透明度下可见。
  • 没有比定义自定义几何图形或自己绘画更简单的方法了。但是,可以在此处部分应用该其他链接中的解决方案 - 在包裹两个矩形的 Item 上设置 layer.enabled: true,在该 Item 上设置不透明度,并删除各个矩形上的不透明度。

标签: qt qml opacity qtquick2


【解决方案1】:

一个解决方案,虽然有点 hacky 是实现你自己的 QML MultiRectangle 组件,它允许设置不透明度并在一堆 QML Rectangle 周围绘制边框

MultiRectangle.qml

import QtQuick 2.0

Item
{
    id: root
    layer.enabled: true

    property int borderWidth: 2
    property color borderColor

    Component
    {
        id: rectangle
        Rectangle{}
    }

    Component.onCompleted:{
        var temp = children.length
        for(var i=0; i<temp; i++)
            rectangle.createObject(this,
                {
                    "z":-100,
                    "anchors.centerIn": children[i],
                    "color": borderColor,
                    "width": children[i].width+borderWidth*2,
                    "height": children[i].height+borderWidth*2,
                    "radius": Math.max((children[i].height+borderWidth*2)
                                       /children[i].height*children[i].radius,
                                     (children[i].height+borderWidth*2)
                                       /children[i].height*children[i].radius)
                })

    }
}

这将在添加到 MultiRectangle 项目的矩形后面动态创建一个伪边框。

示例

import QtQuick 2.5
import QtQuick.Window 2.2
import QtGraphicalEffects 1.0

Window {
    id: root
    visible: true
    height: 200
    width: 400

    RadialGradient {
        anchors.fill: parent
        gradient: Gradient {
            GradientStop { position: 0.0; color: "white"}
            GradientStop { position: 0.3; color: "#444"}
            GradientStop { position: 1; color: "white"}
        }
    }

    MultiRectangle {
        anchors.centerIn: parent
        width: 300
        height: 100
        borderWidth: 2
        borderColor: "red"
        opacity: 0.5

        Rectangle {
            color: "cyan"
            anchors.verticalCenter: parent.verticalCenter
            anchors.left: parent.left
            anchors.leftMargin: parent.borderWidth
            height: parent.height - 2 * parent.borderWidth
            width: height
            radius: height / 2
        }

        Rectangle {
            color: "cyan"
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.margins: parent.borderWidth
            anchors.top: parent.top
            height: 10
            width: height
            radius: height / 2
        }

        Rectangle {
            color: "cyan"
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.horizontalCenterOffset: 30
            anchors.margins: parent.borderWidth
            anchors.top: parent.top
            height: 30
            width: height
            radius: height / 2
        }

        Rectangle {
            color: "cyan"
            anchors.verticalCenter: parent.verticalCenter
            anchors.left: parent.left
            anchors.leftMargin: 50
            height: parent.height * 0.6
            anchors.right: parent.right
            anchors.margins: parent.borderWidth
            radius: height / 2
        }
    }
}

结果

请注意,由于layer.enabled 设置为true,clip 也设置为true。因此,过于靠近 MultiRectangle 边界的子项的边框将被剪裁。

【讨论】:

    猜你喜欢
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 2011-11-13
    • 2012-03-01
    • 2012-01-01
    相关资源
    最近更新 更多