"Object is not defined" 在选项卡视图中

"Object is not defined" in TabView

试图让它在 Qt 5.9 版本中工作

似乎每当我在 QML Tab 中创建一个对象时,父对象(或任何父对象的父对象,等等)都无法识别它。如何让父对象识别选项卡中的对象?或者,如果不可能,那么在选项卡中调用对象函数的变通方法是什么?

我已将代码归结为这个简化示例:

import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 1.4

Window {
    Rectangle {
        id: myFunctionCaller

        Component.onCompleted: {
            myParent.myFunction();
            myChild.myFunction();
            myGrandChild.myFunction();
        }
    }

    TabView {
        id: myParent
        property Rectangle grandchild: myGrandChild
        onGrandchildChanged: console.log("Parent recognizes GrandChild");

        function myFunction() {
            console.log("I'm the Parent");
        }

        Tab{
            id: myChild
            property Rectangle grandchild: myGrandChild
            onGrandchildChanged: console.log("Child recognizes GrandChild");

            function myFunction() {
                console.log("I'm the Child");
            }

            Rectangle {
                id: myGrandChild
                function myFunction() {
                    console.log("I'm the GrandChild");
                }
            }
        }
    }
}

我期望这样的输出:

I'm the Parent
I'm the Child
I'm the GrandChild
Parent recognizes Grandchild
Child recognizes Grandchild

但是,我得到了这个输出:

I'm the Parent
I'm the Child
ReferenceError: myGrandChild is not defined.
ReferenceError: myGrandChild is not defined.
ReferenceError: myGrandChild is not defined.

错误发生在这些行:

myGrandChild.myFunction();
onGrandchildChanged: console.log("Parent recognizes GrandChild");
onGrandchildChanged: console.log("Child recognizes GrandChild");

Tab is a subclass of Loader,因此 Rectangle 直到 Tab 激活后才会创建。

Tabs are lazily loaded; only tabs that have been made current (for example, by clicking on them) will have valid content. You can force loading of tabs by setting the active property to true

因此 id: myGrandChild 仅在加载程序中自动创建的(源)组件中可用。

解决方案是,激活选项卡并用 myChild.item

解决 child

(未测试)