0

我有一个 Flickable 项目,我想在其中放置一个自定义的流组件,所以我创建了这样的 Flickable:

import QtQuick 2.0
import UICore.Layouts 1.0 as Layouts
Flickable{
    anchors.fill: parent;
    contentWidth: parent.width;
    contentHeight: flow.childrenRect.height;

    Component.onCompleted: {
        console.log("The content height is: " + contentHeight);
    }


}

然后在我的 main.qml 中有这个(上面的文件是 MyFlickable,而 MyFlow 是具有特定属性的流):

 MyFlickable{
        ....
        MyFlow{
            id: flow
         ... 
        }
  }

这很好用,但问题是我希望能够以尽可能少的开销重用这个项目。在我的 MyFlow 中设置 id 不起作用,我尝试了这个

contentHeight: contentItem.childrenRect.height

正如这里在 contentWidth 部分中所建议的那样:http: //doc.qt.io/qt-5/qml-qtquick-flickable.html#contentItem-prop

但这总是返回 0。我尝试了其他几种方法来接收 onCompleted 信号,但我得到了同样的运气。这不起作用,例如:

contentHeight: children[0].childrenRect.height

在我看来,这应该与通过 id 访问项目相同,但显然不是。

所以我的问题是:在添加了所有组件之后,我如何达到我的流程高度?

提前致谢!

4

1 回答 1

1

这将实现您想要的:

contentHeight: contentItem.children[0].childrenRect.height

来自Qt 文档

声明为 Flickable 的子项的项自动成为 Flickable 的 contentItem 的父项。在对 Flickable 的孩子进行操作时应该考虑到这一点;通常是相关的 contentItem 的孩子。

但我必须说,组件对自己的 QML 文件之外的事物进行假设是不好的做法,因为这会使组件难以重用。特别是在这种情况下,Flickable 假设它的第一个孩子是使用 childrenRect 属性的 Component 类型。您的 MyFlow 组件可以,但许多其他组件没有,例如 Image。

于 2017-07-14T06:34:41.817 回答