【问题标题】:QML: Show object over non siblingsQML:在非兄弟姐妹上显示对象
【发布时间】:2020-07-27 08:14:01
【问题描述】:

我的 QML 代码中有一个非常难以解决的问题。我会尽可能地总结它,因为它是很长的代码..

我编写了一个颜色选择器 qml 文件,当用户想要选择颜色时调用它。这是在一个大矩形中完成的,其中的小矩形均匀分布,有平面颜色可供选择。

我有一个父矩形,1 个外部中继器,嵌套在这个中继器中的是另一个内部中继器,它连续创建一个小矩形。外部中继器将内部中继器放在另一个中继器下方,这样它就可以用小矩形完全填充矩形,最好使用不同的颜色。

每个小矩形也有一个用动画突出显示自身的功能。这个动画是一个比矩形本身更大的圆。当用户从例如单击颜色时完成此操作。右侧的颜色历史记录,如果存在,它应该突出显示相应的颜色矩形。

现在,问题: 无论我使用什么 z 值,此动画都不会显示在其他矩形上方。它将被相邻的矩形阻挡。我研究过,似乎 z 值不考虑非兄弟姐妹,只考虑单亲中的所有项目。

这里有一些代码可以去掉所有不必要的垃圾。 需要注意的是,每个矩形都有自己的动画和鼠标区域。

import QtQuick 2.12
import QtQuick.Window 2.12

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

    Rectangle {
    id: parentRectangle

    width: 400
    height: 400

    property int numberOfBoxesInARow: 5
    property int numberOfBoxesInAColumn: 5

       Repeater {
          id: outerRepeater

          model: parentRectangle.numberOfBoxesInARow

          Repeater {
             id: innerRepeater
             model: parentRectangle.numberOfBoxesInAColumn

             y: parentRectangle.height / parentRectangle.numberOfBoxesInAColumn * outerIndex
             x: 0

             height: parent.height / parentRectangle.numberOfBoxesInAColumn
             width: parent.width

             property int outerIndex: index

             Rectangle {
                id: individualRectangle
                color: Qt.rgba(1 / (outerIndex + 1), 0, 1 / (index + 1), 1)

                x: parentRectangle.width / parentRectangle.numberOfBoxesInARow * index
                y: outerIndex * parentRectangle.height / parentRectangle.numberOfBoxesInAColumn

                width: parentRectangle.width / parentRectangle.numberOfBoxesInARow
                height: parent.height / parentRectangle.numberOfBoxesInAColumn

                Component.onCompleted: {
                    console.log("Rectangle at " + outerIndex + "|" + index + " created, width / height: " + width.toFixed(2) + "| " + height.toFixed(2))
                }

                MouseArea {
                   anchors.fill: parent
                   onClicked: {
                      highlightAnimation.running = true
                   }
                }

                Rectangle {

                   id: highlightCircle

                   visible: highlightAnimation.running

                   color: 'white'

                   anchors.horizontalCenter: parent.horizontalCenter
                   anchors.verticalCenter: parent.verticalCenter
                   property real size: 0
                   width: size
                   height: size
                   radius: size/2
                }

                PropertyAnimation {
                   id: highlightAnimation
                   target: highlightCircle
                   property: 'size'
                   from: 200
                   to: 0
                   duration: 500
                }
             }
          }
       }
    }
}

【问题讨论】:

  • 请提供minimal reproducible example,而不是一些伪代码。 Rectangle 没有 onClicked 事件你可能错过了什么。
  • 我已经更新了代码,希望拥有完全运行它所需的一切
  • 您是否运行了提供的代码?至于我,我看不到任何可以解释的东西,因为矩形没有填充颜色。 PropertyAnimation.property 应该是字符串,而不是整数。请提供工作示例,否则我们无法运行和测试您的代码。
  • 对不起,这是我在这个网站上的第一篇文章......它现在是一个可运行的 qml 文件,显示了我遇到的问题。如果您运行它并单击一个矩形,它的动画会被在它之后绘制的其他动画阻挡,并且没有 z 值可以解决此问题。谢谢你的帮助,
  • 也许应该是from: individualRectangle.width

标签: qt qml z-index repeater


【解决方案1】:

好的,要将一个项目绘制在另一个项目上,您至少有两种方法:

  • z(仅限同级项目)
  • 创建顺序(最后创建的最高)

我想第二种方式更适合你。所以你只需要在所有其他人之后创建圆圈项目。例如:

Repeater {
    id: outerRepeater       
    Repeater {
        id: innerRepeater
        
        ...    
            MouseArea {
                anchors.fill: parent
                onClicked: {
                    highlightCircle.item = individualRectangle;
                    highlightAnimation.running = true;
                }
            }
        }
    }
}

Rectangle {        
    id: highlightCircle
    property var item
    ...
    anchors.horizontalCenter: item ? item.horizontalCenter : undefined
    anchors.verticalCenter: item ? item.verticalCenter : undefined        
}

PropertyAnimation {
    id: highlightAnimation
    ...
}

【讨论】:

  • 感谢您的回答。我已经尝试过您的解决方案并认为它会起作用,但它给了我错误“QML 矩形:无法锚定到不是父项或兄弟项的项目。”所以不幸的是它不会将自己定位在正确的项目上..
  • 编辑:天哪。这只是一层。我把圆圈往上一层放在窗口的右边,但它没有用,但在外中继器之后向下一层,它就可以工作了。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2019-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-05
  • 2019-08-29
  • 1970-01-01
相关资源
最近更新 更多