QML文字的奇怪行为

时间:2018-06-22 00:11:35

标签: qt qml qtquick2

当我运行以下简单示例时:

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错误地检测到高度: enter image description here

也许是与Text的{​​{1}}有关的问题,所以它对implicitHeight的{​​{1}}有一定的依赖性,但我不明白为什么。

任何人都可以解释一下绑定循环如何出现以及如何解决吗?

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