Qt 为什么在这种情况下会创建循环?

Qt 为什么在这种情况下会创建循环?,qt,qml,qtquickcontrols2,Qt,Qml,Qtquickcontrols2,此示例为我提供了属性绑定错误: file:///home/user/qmltests/layouts.qml:22:4: QML Label: Binding loop detected for property "font.pixelSize" file:///home/user/qmltests/layouts.qml:22:4: QML Label: Binding loop detected for property "font.pixelSize" file:///home/user/

此示例为我提供了属性绑定错误:

file:///home/user/qmltests/layouts.qml:22:4: QML Label: Binding loop detected for property "font.pixelSize"
file:///home/user/qmltests/layouts.qml:22:4: QML Label: Binding loop detected for property "font.pixelSize"
file:///home/user/qmltests/layouts.qml:18:4: QML Label: Binding loop detected for property "font.pixelSize"
代码:

从逻辑上讲,这不应该发生,因为我正在使用
Layout.fillWidth=true
Layout.fillHeight=true
width
height
复制到较低级别的组件

要修复此错误,我必须从根元素复制高度:

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

Page {
    id: root
    width: 400
    height: 200
    StackLayout {
        id: main_container
        Layout.fillWidth:true
        Layout.fillHeight:true
        ColumnLayout {
            id: sub_container
            Layout.fillWidth:true
            Layout.fillHeight:true
            Label {
                text: "One"
                font.pixelSize: root.height*0.2
            }
            Label {
                text: "Two"
                font.pixelSize: root.height*0.2
            }
        }
    }
}
为什么
width
height
没有从
root
元素向下传播到子布局


如何引用
sub_container.width
sub_container.height
(因为在放置项目之前就知道了)而不出现绑定循环错误?我不想引用根项目,因为由于复杂性,根项目内可能有许多布局,为了以可伸缩的方式布局组件,我需要知道父布局的宽度和高度。

如果使用布局,它们管理的元素不得根据大小更改 根据布局给出的尺寸。要执行您希望执行的操作,不应使用布局,而应使用锚定,因为您希望手动管理子尺寸。之所以存在循环,是因为布局使用项目的大小来调整自身大小,然后项目使用该大小来无限地调整自身大小。如果您不需要该功能,它将产生干扰——正如您所看到的。它通过root工作的原因是root的大小不是由布局管理的:它是固定的。这就是你一直想要的,不是吗

另一种方法是标签不根据字体大小更改其大小提示,这样版面就不会对字体大小的更改做出反应


TL;DR:布局根据子布局的大小来调整自身大小,因此如果子布局根据布局的大小来调整自身大小,则会出现一个循环

如果使用布局,则它们所管理的图元不得基于布局更改其大小 根据布局给出的尺寸。要执行您希望执行的操作,不应使用布局,而应使用锚定,因为您希望手动管理子尺寸。之所以存在循环,是因为布局使用项目的大小来调整自身大小,然后项目使用该大小来无限地调整自身大小。如果您不需要该功能,它将产生干扰——正如您所看到的。它通过root工作的原因是root的大小不是由布局管理的:它是固定的。这就是你一直想要的,不是吗

另一种方法是标签不根据字体大小更改其大小提示,这样版面就不会对字体大小的更改做出反应

TL;DR:布局根据子布局的大小来调整自身大小,因此如果子布局根据布局的大小来调整自身大小,则会出现一个循环

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

Page {
    id: root
    width: 400
    height: 200
    StackLayout {
        id: main_container
        Layout.fillWidth:true
        Layout.fillHeight:true
        ColumnLayout {
            id: sub_container
            Layout.fillWidth:true
            Layout.fillHeight:true
            Label {
                text: "One"
                font.pixelSize: root.height*0.2
            }
            Label {
                text: "Two"
                font.pixelSize: root.height*0.2
            }
        }
    }
}