2

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 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

1 Answer 1

2

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");
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

They are not defined anywhere in Rectangle. Because in the first case everything is working ok. But not in the second case. So what should I do, should I declare them somewhere. In the example that you are giving me you are just sending two strings to the function, what I want to do is to invoke the method from c++ with different type of variables. And when I do that I get "QMetaObject::invokeMethod: No such method QDeclarativeRectangle::foo2(QVariant,QVariant)" error And about the quotation marks, I know that I just forgot to put them in the code example before

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.