从父元素访问 Flickable

Access Flickable from parent element

我正在尝试调用在 Flickable 元素内定义的函数,但似乎无法通过 ID 访问它。以下代码在调用 tab.getValues() 时生成 ReferenceError: flickable is not defined

import QtQuick 2.4

Tab {
    id: tab
    function getValues() {
        flickable.getValues()
    }
    Flickable {
        id: flickable
        clip: true
        flickableDirection: Flickable.VerticalFlick
        contentHeight: column.height
        topMargin: 2 * pxPermm
        function getValues() {
            console.log("flickable.getValues() called")
        }
        Column {
            id: column
            // some content
        }
    }
}

我还应该如何访问 Flickable 元素?函数 getValues() 必须放置在 Flickable 中,因为它需要访问放置在列中的元素。

这是 Tab object. It's a Loader whose default property is sourceComponent. When you put a Flickable element inside it, it is converted* to Component and assigned. When your Tab object is viewed, it creates your Flickable object, which can be accessed via item 的问题 属性:

function getValues() {
  if(tab.active)
    tab.item.getValues();
  else
    console.log("Tab is not viewed so item does not exists.");
}

Flickable 对象已继承,但上下文与您的 Tab 对象不同。这意味着您可以从 flickable 访问 tab,但不能从 tab 访问 flickable