qt - 为什么在这种情况下会创建循环?
问题描述
这个例子给了我属性绑定错误:
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将width
and复制height
到较低级别的组件Layout.fillWidth=true
layout.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
到子布局?height
root
如何在不出现绑定循环错误的情况下引用sub_container.width
和sub_container.height
(因为在项目布局之前就知道)?我不想引用根项,因为由于复杂性,根项内可能有许多布局,并且为了以可扩展的方式布置组件,我需要知道父布局的宽度和高度。
解决方案
如果您使用布局,则它们管理的元素不得根据布局给出的大小更改其大小。要执行您想做的事情,您不应该使用布局,而是使用锚点,因为您想手动管理子大小。循环在那里,因为布局使用您的项目的大小来调整自身的大小,然后您的项目使用它来无休止地调整自身的大小。如果您不需要该功能,它会干扰——正如您所见。它通过 root 工作的原因是 root 的大小不受布局管理:它是固定的。这就是你一直想要的,不是吗?
另一种方法是让标签不根据字体大小更改其大小提示,以便布局不会对字体大小更改做出反应。
TL;DR:布局根据子大小自行调整大小,因此如果子根据布局大小调整自身大小,则会出现循环。
推荐阅读
- postgresql - 如何从 TypeORM 连接到 Postgres Amazon-RDS?
- html - 如何使用 flexbox 获取 2 个元素以固定到窗口高度
- ios - Xcode 11 beta 中的“Preview Assets”资产目录有什么用?
- php - 创建目录 PHP / Wordpress Mkdir
- javascript - 如何通过Javascript读取(输入),打印(输出)值到文本文件?
- php - $_SERVER[“DOCUMENT_ROOT”] 为空白,但 phpinfo 显示正确的值。php7
- sql - 使用多个相似的内部查询优化选择查询
- android - 更新 AS 后使用 getContex 和 ContextCompat.checkSelfPermission() 时出错
- bash - 如何判断我的 Perl 程序是否是 shell 逻辑操作的一部分?
- java - 在文本文件中搜索多行字符串