【发布时间】:2018-11-05 02:43:37
【问题描述】:
这个例子给了我属性绑定错误:
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 复制到较低级别的组件
要修复这个错误,我必须从根元素复制高度:
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(因为在项目布局之前就知道)而不会出现绑定循环错误?我不想引用根项,因为由于复杂性,根项内部可能有许多布局,为了以可扩展的方式布置组件,我需要知道父布局的宽度和高度。
【问题讨论】:
标签: qt qml qtquickcontrols2