QML 不透明度继承

发布于 2025-01-03 10:59:07 字数 58 浏览 7 评论 0原文

在 QML 中,如何防止子元素继承其父元素的不透明度? 我想为父元素及其子元素设置不同的不透明度值。

In QML, how can I prevent a child element from inheriting the opacity from its parent?
I want to set different opacity values for the parent and it's child element.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

淑女气质 2025-01-10 10:59:07

我认为,一种方法是使用半透明颜色,如此处 而不是不透明度。

例如,使用四色代码,例如 #800000FF 表示半透明蓝色。

I think, one way would be to use semi transparent colors as described here instead of opacity.

e.g. using quad color code like #800000FF for semi transparent blue.

眼中杀气 2025-01-10 10:59:07

实际上,为父元素设置layer.enabled: true对我来说就是这样。
整个元素被渲染到缓冲区,并且 opacity 被应用到生成的缓冲区(一次到整个图层)。

请参阅http://doc.qt.io/ qt-5/qml-qtquick-item.html#layer.enabled-prop

示例代码:

Rectangle {
    width: 400
    height: 200
    opacity: 0.5
    layer.enabled: true
    Rectangle {
            width: parent.width
            height: parent.height
            color: 'red'
    }
    Rectangle {
            width: parent.width / 2
            height: parent.height
            color: 'blue'
    }
}

这是一个解决方案,但请确保您知道启用分层时在做什么。

另一种可能的解决方案是使用着色器效果。

感谢 #qt@freenode 上的 peppe。

Actually, setting layer.enabled: true for the parent element does the thing for me.
The whole element is rendered to the buffer, and opacity is applied to the resulting buffer (to the whole layer at once).

See http://doc.qt.io/qt-5/qml-qtquick-item.html#layer.enabled-prop

Example code:

Rectangle {
    width: 400
    height: 200
    opacity: 0.5
    layer.enabled: true
    Rectangle {
            width: parent.width
            height: parent.height
            color: 'red'
    }
    Rectangle {
            width: parent.width / 2
            height: parent.height
            color: 'blue'
    }
}

That is a solution, but make sure that you know what you are doing when enabling layering.

Another possible solution would be using a shadereffect.

Thanks to peppe on #qt@freenode.

昵称有卵用 2025-01-10 10:59:07

你不能。项目的不透明度值是相对于其父级的,因此如果您编写类似的代码,

Rectangle {
  color: "red"
  opacity: 0.5
  width: 200; height: 100

  Rectangle {
    color: "blue"
    opacity: 1
    width: 100; height: 100
  }
}

您将看到两个矩形具有相同的不透明度。

You can't. Items opacity value is relative to their parents one, so if you code something like

Rectangle {
  color: "red"
  opacity: 0.5
  width: 200; height: 100

  Rectangle {
    color: "blue"
    opacity: 1
    width: 100; height: 100
  }
}

You will see that the two rectangles have the same opacity.

浅浅 2025-01-10 10:59:07

我刚才遇到了这个问题。使用 Qt 5.1.0

在我的例子中,我有一个 矩形 元素,其 opacity: 0.6 和一个子 Image 元素。 Image 继承了透明度 - 这不是我们想要的。

为了解决这个问题,我将主 Rectangle 包含在 Item 元素中。将大小/位置定义从 Rectangle 传递到外部 Item。将Image移至矩形之外。

最后,我将 Item 作为主要父级,并在 Item 内并排放置 RectangleImage

只有 Rectangle 保持不透明度 0.6,因此 Rectangle 具有透明度,而 Image 完全不透明。

I've bumped into this issue just now. Using Qt 5.1.0

In my case, I had a Rectangle Element with opacity: 0.6 and a child Image element. The Image was inheriting the transparency - not desired.

To solve it, I enclosed the main Rectangle in an Item element. Passed the size/position definitions from the Rectangle to the outer Item. Moved the Image outside the Rectangle.

In the end, I had Item as the main parent and Rectangle and Image side by side, inside Item.

Only Rectangle maintained the opacity 0.6, so the Rectangle has transparency and Image is fully opaque.

向日葵 2025-01-10 10:59:07

这是可能的!您需要在 Component.onCompleted 范围内测试父级的不透明度。如果其值为 0,则需要将对象的父级更改为其当前父级的父级。

例子:

Item{
    id:root

    Item{
        id:currentParent
        opacity: 0
        Item{
            id:item
            Component.onCompleted:{
                if(parent.opacity === 0)
                    item.parent = currentParent.parent
            }
        }
    }
}

It's possible! You need to test in the Component.onCompleted scope the opacity of the parent. If its 0 you need to change the parent of your object to the parent of it's current parent.

Example:

Item{
    id:root

    Item{
        id:currentParent
        opacity: 0
        Item{
            id:item
            Component.onCompleted:{
                if(parent.opacity === 0)
                    item.parent = currentParent.parent
            }
        }
    }
}
撩人痒 2025-01-10 10:59:07

您无法阻止子元素从其父元素继承不透明度。

我个人的解决方法是将其更改为:

Rectangle {
    color: "red"
    opacity: 0.5
    width: 200; height: 100

    Rectangle {
        color: "blue"
        opacity: 1
        width: 100; height: 100
    }
}

更改为:

Item {
    width: 200; height: 100

    Rectangle {
        anchors.fill: parent
        color: "red"
        opacity: 0.5
    }

    Rectangle {
        color: "blue"
        opacity: 1
        width: 100; height: 100
    }

}

或此(仅当父级为纯色时才可能):

Rectangle {
    color: "#7FFF0000" // 50% transparent red
    opacity: 0.5
    width: 200; height: 100

    Rectangle {
        color: "blue"
        opacity: 1
        width: 100; height: 100
    }
}

You cannot prevent the child element from inheriting the opacity from its parent.

My personal work around is to change this:

Rectangle {
    color: "red"
    opacity: 0.5
    width: 200; height: 100

    Rectangle {
        color: "blue"
        opacity: 1
        width: 100; height: 100
    }
}

Into this:

Item {
    width: 200; height: 100

    Rectangle {
        anchors.fill: parent
        color: "red"
        opacity: 0.5
    }

    Rectangle {
        color: "blue"
        opacity: 1
        width: 100; height: 100
    }

}

or this (only possible if the parent is a solid color):

Rectangle {
    color: "#7FFF0000" // 50% transparent red
    opacity: 0.5
    width: 200; height: 100

    Rectangle {
        color: "blue"
        opacity: 1
        width: 100; height: 100
    }
}
一人独醉 2025-01-10 10:59:07

我认为这是不可能的。您必须使两个元素成为兄弟元素并根据需要更改其不透明度。

I don't think its possible. you have to make two element sibling and changes its opacity as you wish.

记忆里有你的影子 2025-01-10 10:59:07

不可能这样做,但您可以使用 Qt.lighter(color,opacity) 更改它们

颜色

Rectangle {
  color: Qt.lighter("red",.5)
  width: 200; height: 100

  Rectangle {
    color: Qt.lighter("blue",1)
    width: 100; height: 100
  }
}

it is not possible to do that but you can change their color with

Qt.lighter(color,opacity)

for example

Rectangle {
  color: Qt.lighter("red",.5)
  width: 200; height: 100

  Rectangle {
    color: Qt.lighter("blue",1)
    width: 100; height: 100
  }
}
在巴黎塔顶看东京樱花 2025-01-10 10:59:07

我在 Qt 4.8.6 中也遇到了这个问题。

在我的特定情况下,我希望顶级项目的透明度为 20%,黑色,但其子元素不受父元素的任何不透明度/透明度设置的影响。

由于 QML 的继承机制,不透明度不起作用。

但我能够使用 Qml Qt 对象中的 rgba 函数。这让我得到了我想要的,父元素现在是 20% 透明,但子元素不受影响。

Rectangle {
    width: 400
    height: 400
    color: Qt.rgba(0, 0, 0, 0.2) // Works perfectly, pure black with 20% transparency, equal to 0.2 opacity

    // Unaffacted child elements here...
}

注意:我也尝试过直接使用 RGBA 颜色代码,正如之前的海报所提到的,但它不起作用。

示例:

color: "#000000FA" // Supposed to be semi transparent, but always transparent, regardless of the alpha value

为任何其他 RGBA 值设置 alpha 值都有效,但纯黑色不起作用。

I also ran into this problem with Qt 4.8.6.

In my particular case, I wanted the top level item to be 20% transparent with black color, but have its child elements be unaffected by any opacity/transparency setting from the parent.

Opacity did not work, due to the inheritance mechanism of QML.

But I was able to use the rgba function from the Qml Qt object. This allowed me to get exactly what I wanted, the parent is now 20% transparent, but the child elements are unaffected.

Rectangle {
    width: 400
    height: 400
    color: Qt.rgba(0, 0, 0, 0.2) // Works perfectly, pure black with 20% transparency, equal to 0.2 opacity

    // Unaffacted child elements here...
}

Note: I also tried to use the RGBA color codes directly, as mentioned by a previous poster, but it did not work.

Example:

color: "#000000FA" // Supposed to be semi transparent, but always transparent, regardless of the alpha value

Setting the alpha value for any other RGBA values worked, just not with pure black.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文