首页 > 解决方案 > 为什么在这种情况下会创建循环?

问题描述

这个例子给了我属性绑定错误:

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"

代码:

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: sub_container.height*0.2
            }
            Label {
                text: "Two"
                font.pixelSize: sub_container.height*0.2
            }
        }
    }
}

按逻辑,这不应该发生,因为我通过使用and将widthand复制height到较低级别的组件Layout.fillWidth=truelayout.fillHeight=true

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

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到子布局?heightroot

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

标签: qtqmlqtquickcontrols2

解决方案


如果您使用布局,则它们管理的元素不得根据布局给出的大小更改其大小。要执行您想做的事情,您不应该使用布局,而是使用锚点,因为您想手动管理子大小。循环在那里,因为布局使用您的项目的大小来调整自身的大小,然后您的项目使用它来无休止地调整自身的大小。如果您不需要该功能,它会干扰——正如您所见。它通过 root 工作的原因是 root 的大小不受布局管理:它是固定的。这就是你一直想要的,不是吗?

另一种方法是让标签不根据字体大小更改其大小提示,以便布局不会对字体大小更改做出反应。

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


推荐阅读