【发布时间】:2016-06-30 10:57:56
【问题描述】:
我需要一些像素完美的元素,以及其他应该可调整大小的元素,所以我尽量避免布局,只使用锚点。
如果我有一个固定大小的项目作为“主画布”,我可以通过锚定做任何必要的事情来实现我的目标。但是,它不适用于主窗口。如果我有一个锚定到主窗口的项目,它的大小报告为零
ApplicationWindow
{
id: mainWindow
width: 1024
height: 768
minimumWidth: 800
minimumHeight: 480
Component.onCompleted: { console.log("mainWindow width: " + width) }
Item
{
id: topLevelItem
anchors.fill: parent
Component.onCompleted: { console.log("topLevelItem width: " + width) }
}
}
有趣的是,topLevelItem 的宽度打印为0,而mainWindow 的宽度正确打印为1024。
我添加了一些元素,现在变得很奇怪:
ApplicationWindow
{
id: mainWindow
width: 1024
height: 768
minimumWidth: 800
minimumHeight: 480
Component.onCompleted: { console.log("mainWindow width: " + width) }
Item
{
id: topLevelItem
anchors.fill: parent
Component.onCompleted: { console.log("topLevelItem width: " + width) }
Rectangle
{
id: red
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.left: parent.left
width: 100
color: "#FF0000"
Component.onCompleted: { console.log("red width: " + width) }
}
Rectangle
{
id: green
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.left: red.right
anchors.right: blue.left
color: "#00FF00"
Component.onCompleted: { console.log("green width: " + width) }
}
Rectangle
{
id: blue
anchors.top: parent.top
anchors.bottom: parent.bottom
anchors.right: parent.right
width: 50
color: "#0000FF"
Component.onCompleted: { console.log("blue width: " + width) }
}
}
}
一切都按原样显示。左边有一个 100 px 的垂直红条,右边有一个 50 px 的垂直蓝条,其余的用绿色填充。
令人惊讶的是,尺寸不对:
qml: topLevelItem width: 0
qml: blue width: 50
qml: green width: -150
qml: red width: 100
qml: mainWindow width: 1024
除mainWindow 外,所有内容的高度都报告为零。
我需要访问尺寸,尤其是中间区域的尺寸,因为它必须根据可用空间影响将放置什么样的其他元素。
这种奇怪行为的原因是什么,我做错了什么吗?如何正确获取中间矩形的大小?
【问题讨论】:
标签: qt user-interface qml