ListView委托上的绑定循环

时间:2019-03-08 19:58:59

标签: qt qml qtquickcontrols2

我有这段代码,这给了我这个错误:

file:///home/user/qmltests/bindingheight.qml:29:5: QML Pane: Binding loop detected for property "height"

为什么会产生循环?以及我该如何解决?

import QtQuick 2.11
import QtQuick.Controls 2.4
import QtQuick.Layouts 1.11


ListView {
            width: 300
            height: 600
            clip: true
            model: ListModel {
                id: fruitModel

                ListElement {
                    name: "Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple Apple"
                    cost: 2.45
                }
                ListElement {
                    name: "Orange Orange Orange Orange Orange Orange Orange Orange Orange Orange"
                    cost: 3.25
                }
                ListElement {
                    name: "Banana Banana Banana Banana Banana Banana Banana Banana Banana Banana"
                    cost: 1.95
                }
            }
            delegate: Item {
                width: 200
                height: childrenRect.height * 1.1
                Pane {
                    height: contentItem.childrenRect.height
                    anchors.left: parent.left
                    anchors.right: parent.right
                    anchors.bottomMargin: 20
                    padding: 10
                    background: Rectangle { anchors.fill: parent; color: "gray";}
                    MouseArea {
                        anchors.fill: parent
                        onClicked: {
                            console.log("Executing action on the item")
                        }   
                    }
                    ColumnLayout {
                        anchors.top: parent.top
                        anchors.left:parent.left
                        anchors.right: parent.right
                        Label {
                            id: item_to_hide
                            text: model.name
                            wrapMode: Label.WordWrap
                            Layout.maximumWidth: parent.width
                        }
                        Label {
                            text: model.cost
                        }
                        Button {
                            text: " Hide element "
                            onClicked: {
                                item_to_hide.visible=false
                            }
                        }
                        Button {
                            text: " Show element "
                            onClicked: {
                                item_to_hide.visible=true
                            }
                        }
                    }
                }
            }
}

这是在Qt 5.11中进行的,测试是使用qmlscene完成的,只需复制/粘贴并输入到bin / qmlscene中即可重现错误。

1 个答案:

答案 0 :(得分:2)

使用您编写的相同代码,只需将height的{​​{1}}更改为Pane。这将打破implicitHeight创建的循环。

height

...