简体   繁体   English

向非父对象调用函数

[英]Call function to a non parent object

Lets say I have a QML that contains this: 可以说我有一个包含以下内容的QML:

MainView {
    id: main
    Component {
        MyItem {
            // has function doSomething()
            id: item
            function doSomething() // this will work
            Child {
                function parent.doSomething() // this too
                function item.doSomething() // and this
            }
        }
    }
    Page {
        // here is where I would like to be able to call the function
    }
}

I'm unable to call the function doSomething() from anything that's not a child, is it possible to call it for example from inside the Page {} object? 我无法从不是孩子的任何对象调用函数doSomething() ,例如可以从Page {}对象内部调用它吗?

I'm sorry for the question being so badly written, but I'm unsure of how to put it in another way. 对于如此糟糕的问题,我感到抱歉,但是我不确定如何用另一种方式来表达它。 Thanks for your time. 谢谢你的时间。

That's because you wrapped into a Component item, which acts like a separated file sandboxing the var/function scope. 那是因为您包装了一个Component项,它的作用就像是将var / function范围的单独文件沙箱化。 So from inside the component you can access the outside but the opposite is impossible. 因此,您可以从组件内部访问外部,但相反是不可能的。 The only trick to acheive what you're trying to do would be to declare a signal in an outside element, add a Connections item inside your component and fire you signal when you need to trigger the inner function. 实现您要执行的操作的唯一技巧是在外部元素中声明信号,在组件内部添加Connections项,并在需要触发内部函数时触发信号。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM