为什么在这种情况下会创建循环?
Why is loop created in this case?
这个例子给我 属性 绑定错误:
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
}
}
}
}
按照逻辑,这不应该发生,因为我正在使用 Layout.fillWidth=true
和 layout.fillHeight=true
[= 将 width
和 height
复制到较低级别的组件22=]
要修复此错误,我必须从根元素复制高度:
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:布局根据子尺寸调整自身尺寸,因此如果子尺寸根据布局尺寸调整自身尺寸,则会出现循环。
这个例子给我 属性 绑定错误:
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
}
}
}
}
按照逻辑,这不应该发生,因为我正在使用 Layout.fillWidth=true
和 layout.fillHeight=true
[= 将 width
和 height
复制到较低级别的组件22=]
要修复此错误,我必须从根元素复制高度:
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:布局根据子尺寸调整自身尺寸,因此如果子尺寸根据布局尺寸调整自身尺寸,则会出现循环。