QML anchors to ApplicationWindow via id not work

2019-02-22 04:54发布

I test a simple QML(Qt sdk version 5.3.2) program like this

import QtQuick 2.3
import QtQuick.Controls 1.2

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("Hello World")
    id: appWin

    Text {
        text: qsTr("Hello World")
        anchors.bottom: parent.bottom
    }
}

I want the text will be layed on the bottom of application window, that's work. But if I change anchors.bottom: parent.bottom to anchors.bottom: appWin.bottom (via id), anchors not work anymore, is this a bug?

标签: c++ qt qml
2条回答
在下西门庆
2楼-- · 2019-02-22 05:42

This looks like a bug, but it can be by design according following docs:

For performance reasons, you can only anchor an item to its siblings and direct parent

It's not really clear how we can specify direct parent when anchoring. QML translator may only accept parent to reference parent qml item.

查看更多
相关推荐>>
3楼-- · 2019-02-22 05:54

ApplicationWindow does not ultimately derive from Item, so it does not have an anchors property that's why using the window's id does not work. So why does using parent? Because the children you define in an ApplicationWindow become children of an intermediate Item called contentItem:

If you assign an Item to the data list, it becomes a child of the Window's contentItem, so that it appears inside the window. The item's parent will be the window's contentItem, which is the root of the Item ownership tree within that Window. ... It should not generally be necessary to refer to the data property, as it is the default property for Window and thus all child items are automatically assigned to this property.

查看更多
登录 后发表回答