开发者

Calling methods in qml - javascript from c++. Scope problem

I want to call a method in qml - javascript from c++. Basically I think that I have done everything that is said in documentation. I can 开发者_C百科call the method if it is like this:

Rectangle {
......
    Component.onCompleted:{
    ...........
    }

    function foo(arg1, arg2)
    {
        ................
    }
}

But I can't call the same function if I put it like this and in a separate .js file it like this:

import ../Script.js as Script
Rectangle {
    .........

    Component.onCompleted:{
       Script.foo(arg1,arg2)
    }

}

The script is imported and everything, but I still have a problem that says that the arguments are not recognized. Any help will be appreciated. Thanks


Are arg1 and arg2 defined somewhere in your Rectangle?

Else it should work, except that you have to import the Script.js with quotation marks

import "../Script.js" as Script

For testing I used

Script.js

function foo(arg1, arg2) {
    print(arg1, arg2)
}

main.qml

import QtQuick 1.0

import "Script.js" as Script

Rectangle {
    width: 360
    height: 360

    Component.onCompleted: {
        Script.foo("a", "b");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜