QML通过id锚定到ApplicationWindow不工作 [英] QML anchors to ApplicationWindow via id not work

查看:469
本文介绍了QML通过id锚定到ApplicationWindow不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我测试一个简单的QML(Qt sdk版本5.3.2)程序像这样

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
    }
}

我想要的文本将被放置在应用程序窗口的底部,这是工作。但如果我将 anchors.bottom:parent.bottom 更改为 anchors.bottom:appWin.bottom (通过id)锚点不再工作,这是一个错误吗?

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?

推荐答案

ApplicationWindow 不是最终派生自 Item ,所以它没有 anchors 属性,这就是为什么使用窗口的 id 不起作用。那么为什么使用 parent ?因为孩子 中定义ApplicationWindow 成为中间体的子项 调用 contentItem

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:


如果将项目分配给数据列表,它将成为
Window的contentItem的子项,它出现在窗口内。项目的
父项将是窗口的contentItem,它是该窗口中项目
所有权树的根。 ...通常不需要
来引用data属性,因为它是Window的默认属性
,因此所有子项目都自动分配给此
属性。 p>

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.

这篇关于QML通过id锚定到ApplicationWindow不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆