前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >关于Qml的Window控件不能使用id进行布局定位的问题

关于Qml的Window控件不能使用id进行布局定位的问题

作者头像
Qt君
发布2019-07-15 16:40:56
1.2K0
发布2019-07-15 16:40:56
举报
文章被收录于专栏:跟Qt君学编程跟Qt君学编程

解决Qml的Window控件不能使用id进行布局定位的问题。

问题重现

  • 运行后Rectangle并不能按照预想的置于底部行为,而是布局不变(默认布局顶部)。
代码语言:javascript
复制
Window {
    id: root
    visible: true
    width: 640
    height: 480

    Rectangle {
        id: rect
        width: 100; height: 100
        anchors.bottom: root.bottom
        color: "red"
    }
}

分析

  • 先查看Qt官方文档找找线索,既然布局不了就找 anchors布局的文档,通过全局搜索文档找到了以下信息。
代码语言:javascript
复制
Note: You can only anchor an item to siblings or a parent. 
注意:只能将项目锚定到同级或父级。
  • 由此可以推测Window的 rootid所指向的并不是派生于Item(或QQuickItem)的。
  • 通过打印Window的id与parent属性分别为QQuickWindowQmlImpl与QQuickRootItem,可以肯定的是 QQuickWindowQmlImpl不是继承于QQuickItem,导致布局不到的问题。
  • 感觉那里不对,又找了下文档,看到以下信息:
代码语言:javascript
复制
[default] data : list<Object>

The data property allows you to freely mix visual children, resources and other Windows in a Window.
If you assign another Window to the data list, the nested window will become "transient for" the outer Window.
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.
If you assign any other object type, it is added as a resource.
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.

data属性允许您在Window中自由混合可视子项,资源和其他Windows。
如果将另一个窗口分配给数据列表,嵌套窗口将变为"瞬态"外部窗口。
如果将一个Item分配给数据列表,它将成为Window的contentItem的子项,以便它出现在窗口内。 项目的父项将是窗口的contentItem,它是该窗口中项目所有权树的根。
如果指定任何其他对象类型,则将其添加为资源。
通常不需要引用data属性,因为它是Window的默认属性,因此所有子项都会自动分配给此属性。
  • 大概意思是Window窗口的根Item被附加在 contentItem上。

解决方法

  • 使用 parent或 root.contentItem或 Window.contentItem替换 root作为布局的锚。
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-06-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Qt君 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 问题重现
  • 分析
  • 解决方法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档