gpt4 book ai didi

qt - QML - 堆叠元素的不透明度

转载 作者:行者123 更新时间:2023-12-02 04:09:06 25 4
gpt4 key购买 nike

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

Stacked Opacity

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

这是代码:

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"
}
}
}

最佳答案

一个解决方案,尽管有点 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
}
}
}

结果

Screenshot of the qmlscene running the example

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

关于qt - QML - 堆叠元素的不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37763485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com