Qt World Summit: Submit your Presentation

Obtain return value of a JS function, QtWebkit bridge

  • I'm (trying) developing a wrap for a JS framework and my problem is to access the values returned by JS functions in the original framework.

    The idea would be to connect the C++ method with the JS function but in order to do so the method has to be a signal and signals can not handle return values. The proper way to do so is to use the macro Q_INVOKABLE but in that case I can not connect it to the JS function.

    The JS code would look like something like this
    //Somewhere in the JS framework
    JSFramework.myclass.prototype = {
    equals : function(obj) {
    if(!obj) { return false;}
    return true
    //more methods...

    And my C++ code would look like this
    class myclass : public QObject {



    //Invocable method can handle return values according to documentation.
    //But they can not be connected to js functions
    Q_INVOKABLE bool invokable_equals(myclass* obj);


    //Signals can be connected to js method but can not handle return values
    bool signal_equals(myclass* obj);

    So what I am trying to do is to call the JS function equals when the the C++ method is called. Signals can be connected to JS functions but can not handle return values. Invokable methods can not be connected to JS functions but can handle return values.
    //In the js
    myobject_name_on_js.signal_equals.connect(equals) //Ok
    myobject_name_on_js.invokable_equals.connect(equals) //Error, invokable_equals() is not a signal
    ¿Any ideas about this issue?

  • I don't quite understand your question as I'm just learning the bridge technology myself and have not attempted to create signals or slots connected to js but I have been able to get return values using Q_INVOKABLE functions in js for primitives such as strings. The method I used is as follows:

    Create a Object such as
    @class myClass : public QObject
    explicit myClass(QObject *parent=0);
    Q_INVOKABLE QString myMethod(QString text);

    Connect the QWebFrame signal javaScriptWindowObjectCleared() to a slot that includes code like

    @myClass *myOBJ = new myClass(this);

    In your js myOBJ can be treated as any other js object. For example:
    @var myString = myOBJ.myMethod(text)@

    will return a string that is a function of how the string "text" is manipulated by myMethod. This works for boolean and integer primitives as well.

    According to the "documentation":http://harmattan-dev.nokia.com/docs/library/html/qt4/qtwebkit-bridge.html this method should be valid for other data types and even containers (e.g., QVariantMap) also but I haven't been successful in using it to obtain things like lists (especially node lists) yet but am working on it.

    Hope the above helps and look forward to you posting your solution.

  • My problem is that I need to recover the value returned by a js fuction from a C++ method and connect that method to a js function.

    bq. In your js myOBJ can be treated as any other js object. For example:
    @var myString = myOBJ.myMethod(text)@

    In the previous example, your js variable will store the value returned by a C++ method and my problem is just the other way around.

    I need to wrap a js "class" which has its own methods. It's easy to do so with events (calling a C++ method from JS function) and methods that do not return a value (calling a JS function from a C++ method). The problem is, on the second case, if the JS function returns a value and I need that value on the C++ side I can not access that value because it's done by connection signals to js functions and signals can not handle return values.

    In sumary

    • I need to connect a C++ method with a JS function
    • I need the value returned by the JS function to by returned by my C++ method

  • If I understand correctly I think what can help you is the evaluateJavaScript method which returns the js function return value as a QVariant.

Log in to reply