首页 文章

QML通过id锚定到ApplicationWindow不起作用

提问于
浏览
6

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

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),锚点不再起作用,这是一个错误吗?

2 回答

  • -2

    ApplicationWindow 最终并非来自 Item ,因此它没有's why using the window' s id 不起作用的 anchors 属性 . 那么为什么要使用 parent ?因为您在 ApplicationWindow 中定义的子项名为children of an intermediate Item ,称为 contentItem

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

  • 7

    这看起来像一个bug,但它可以按照以下设计docs

    出于性能原因,您只能将项目锚定到其兄弟姐妹和直接父母

    在锚定时我们如何指定 direct parent 并不是很清楚 . QML翻译者只能接受 parent 来引用父qml项目 .

相关问题