Layout 设置定位时,QML忽略宽度和高度

Layout 设置定位时,QML忽略宽度和高度,layout,qml,qtquick2,Layout,Qml,Qtquick2,我试图了解锚在QML(QtQuick2.0)中是如何工作的。我有一个简单的项目如下: AddButton.qml: Item { Button { text: "ADD" width: 100 height: 50 } } Item { id: root width: 800 height: 600 AddButton { id: addButton } } Ite

我试图了解锚在QML(QtQuick2.0)中是如何工作的。我有一个简单的项目如下:

AddButton.qml:

Item {

    Button {
        text: "ADD"
        width: 100
        height: 50
    }

}
Item {
    id: root
    width: 800
    height: 600

    AddButton {
        id: addButton
    }

}
Item {

    .....

    AddButton {
        id: addButton
        anchors.right: parent.right
        anchors.bottom: parent.bottom
    }

}
Item {

    .....

    AddButton {
        id: addButton
        width: 100
        height: 50
        anchors.right: parent.right
        anchors.bottom: parent.bottom
    }

}
我将其添加到主QML文件中,如下所示:

main.qml:

Item {

    Button {
        text: "ADD"
        width: 100
        height: 50
    }

}
Item {
    id: root
    width: 800
    height: 600

    AddButton {
        id: addButton
    }

}
Item {

    .....

    AddButton {
        id: addButton
        anchors.right: parent.right
        anchors.bottom: parent.bottom
    }

}
Item {

    .....

    AddButton {
        id: addButton
        width: 100
        height: 50
        anchors.right: parent.right
        anchors.bottom: parent.bottom
    }

}
这个很好用。但是,当我尝试使用锚将按钮放在右下角时,按钮就会消失:

main.qml:

Item {

    Button {
        text: "ADD"
        width: 100
        height: 50
    }

}
Item {
    id: root
    width: 800
    height: 600

    AddButton {
        id: addButton
    }

}
Item {

    .....

    AddButton {
        id: addButton
        anchors.right: parent.right
        anchors.bottom: parent.bottom
    }

}
Item {

    .....

    AddButton {
        id: addButton
        width: 100
        height: 50
        anchors.right: parent.right
        anchors.bottom: parent.bottom
    }

}
只有在主QML文件级别设置宽度和高度时,才会返回:

main.qml:

Item {

    Button {
        text: "ADD"
        width: 100
        height: 50
    }

}
Item {
    id: root
    width: 800
    height: 600

    AddButton {
        id: addButton
    }

}
Item {

    .....

    AddButton {
        id: addButton
        anchors.right: parent.right
        anchors.bottom: parent.bottom
    }

}
Item {

    .....

    AddButton {
        id: addButton
        width: 100
        height: 50
        anchors.right: parent.right
        anchors.bottom: parent.bottom
    }

}

所以我想知道,为什么当我设置锚时,按钮会消失?有没有办法不在主QML文件中设置宽度和高度(基本上是使用AddButton.QML中设置的任何大小?

问题是封装的
没有明确的
宽度
高度
。在这种情况下,引擎引用“自然”witdh/高度,即
隐式宽度
/
隐式高度
属性。在大多数情况下,即使在这个特定的情况下,这些属性也恰好为零。因此,自定义类型的维度为零。 因此,
AddButton.anchors.bottom
实际上位于封装的
按钮的最顶部,该按钮反过来突出了封装的

这有两件事:

除非要隐藏
按钮
的内部,否则不需要用项封装
按钮

如果后者是您的愿望,请尝试以下方法:

Item {
    width: 100 //give the object a dimension!
    height: 50

    Button {
        text: "ADD"
        anchors.fill: parent
    }
}

现在您可以锚定它,它将不会被定位到其他地方。

问题是封装的
没有明确的
宽度
高度
。在这种情况下,引擎引用“自然”witdh/高度,即
隐式宽度
/
隐式高度
属性。在大多数情况下,即使在这个特定的情况下,这些属性也恰好为零。因此,自定义类型的维度为零。 因此,
AddButton.anchors.bottom
实际上位于封装的
按钮的最顶部,该按钮反过来突出了封装的

这有两件事:

除非要隐藏
按钮
的内部,否则不需要用项封装
按钮

如果后者是您的愿望,请尝试以下方法:

Item {
    width: 100 //give the object a dimension!
    height: 50

    Button {
        text: "ADD"
        anchors.fill: parent
    }
}

现在可以锚定它,它不会在其他地方被定位。<代码>隐式属性总是被忽略,但它们非常重要,所以考虑将它们与新手讨论。<代码>隐式属性总是被忽略,但它们非常重要,所以考虑向新手讨论它们。