当我运行以下简单示例时:
import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Window 2.2
import QtQuick.Layouts 1.3
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Popup {
width: 100
visible: true
contentItem: ColumnLayout {
Rectangle {
Layout.fillWidth: true
implicitHeight: 50
color: "red"
}
Text {
Layout.fillWidth: true
text: "some very very very very very very very very very very very very long text"
wrapMode: Text.WordWrap
}
}
}
}
我收到以下警告:qrc:/main.qml:12:5: QML Popup: Binding loop detected for property "contentHeight"
和Popup
错误地检测到高度:
也许是与Text
的{{1}}有关的问题,所以它对implicitHeight
的{{1}}有一定的依赖性,但我不明白为什么。
任何人都可以解释一下绑定循环如何出现以及如何解决吗?
答案 0 :(得分:2)
解决方案:
Popup {
width: 100
visible: true
contentItem: Column {
Rectangle {
width: parent.width
implicitHeight: 50
color: "red"
}
Text {
width: parent.width
text: "some very very very very very very very very very very very very long text"
wrapMode: Text.WordWrap
}
}
}
关于警告: 它看起来像个错误,但我想不是。如果两个直肠都不会发出警告。
删除Layout.fillWidth: true
的文本也将没有。
因此,Text的高度需要ColumnLayout的宽度,而ColumnLayout可以通过Text的高度来扩展高度。感觉像一个悖论,两个组成部分需要彼此大小。
更新:
Popup {
width: 100
visible: true
contentItem: ColumnLayout {
width: parent.width
Rectangle {
Layout.fillWidth: true
implicitHeight: 50
color: "red"
}
Text {
Layout.fillWidth: true
text: "some very very very very very very very very very very very very long text"
wrapMode: Text.WordWrap
}
}
}