在 qml 中使用锚点时未设置坐标
Coordinates not set when using anchors in qml
我在使用 qml 时遇到了一个问题,即当我使用锚点来布置项目时,它们的坐标没有被设置并且等于零。请参阅下面的代码片段。
QML 锚点的这种行为是正常的还是我做错了什么?
如何规避?
Rectangle
{
id: background
objectName: "background"
anchors.fill: parent
color: "#06A0D4"
}
Rectangle
{
id: ground
objectName: "ground"
anchors.left: background.left
anchors.right: background.right
anchors.bottom: background.bottom
color: "#D47006"
opacity: 0.4
height: 50;
}
谢谢。
这应该有效,即当我在 qmlscene 中 运行 以下代码时,文本显示 Yellow rect: x=0, y=350, width=400, height=50
,这正是我所期望的
import QtQuick 2.0
Item {
width: 400
height: 400
Rectangle {
id: background
anchors.fill: parent
color: "black"
}
Rectangle {
anchors.left: background.left
anchors.right: background.right
anchors.bottom: background.bottom
height: 50
color: "yellow"
Text {
anchors.centerIn: parent
text: "Yellow rect: x=" + parent.x + ", y=" + parent.y +
", width=" + parent.width + ", height=" + parent.height
}
}
}
这是真的,它是未定义的。
我想问题出在 mapToItem
内。 mapFromItem
函数,需要一个项目作为它的第一个参数。
如果没有明确设置,我只能猜测这些函数或相关函数用于计算坐标。
一个Window
对象不是Item,而是一个QQuickWindow,因此那些函数将不起作用。
将所有内容包裹在 Item
中应该可以解决问题,因为现在 QML 可以使用它的函数计算位置。
我在使用 qml 时遇到了一个问题,即当我使用锚点来布置项目时,它们的坐标没有被设置并且等于零。请参阅下面的代码片段。
QML 锚点的这种行为是正常的还是我做错了什么? 如何规避?
Rectangle
{
id: background
objectName: "background"
anchors.fill: parent
color: "#06A0D4"
}
Rectangle
{
id: ground
objectName: "ground"
anchors.left: background.left
anchors.right: background.right
anchors.bottom: background.bottom
color: "#D47006"
opacity: 0.4
height: 50;
}
谢谢。
这应该有效,即当我在 qmlscene 中 运行 以下代码时,文本显示 Yellow rect: x=0, y=350, width=400, height=50
,这正是我所期望的
import QtQuick 2.0
Item {
width: 400
height: 400
Rectangle {
id: background
anchors.fill: parent
color: "black"
}
Rectangle {
anchors.left: background.left
anchors.right: background.right
anchors.bottom: background.bottom
height: 50
color: "yellow"
Text {
anchors.centerIn: parent
text: "Yellow rect: x=" + parent.x + ", y=" + parent.y +
", width=" + parent.width + ", height=" + parent.height
}
}
}
这是真的,它是未定义的。
我想问题出在 mapToItem
内。 mapFromItem
函数,需要一个项目作为它的第一个参数。
如果没有明确设置,我只能猜测这些函数或相关函数用于计算坐标。
一个Window
对象不是Item,而是一个QQuickWindow,因此那些函数将不起作用。
将所有内容包裹在 Item
中应该可以解决问题,因为现在 QML 可以使用它的函数计算位置。