[Qt-interest] How to calling javascript callback from Qt/C++

Joshua Lee muzili at gmail.com
Mon Dec 13 11:02:04 CET 2010


Hi All,
I have a question about Qt hybrid programing. Here's my request:
1. From the javscript, i define the callback.
<code>
  function testapi_cb()
  {
    var result = arguments[0].value;
    alert(result);
    for (i = 1; i < arguments.length; i++)
    {
      alert(arguments[i].value);
    }
    //handle the result and input
  }
</code>
2. Calling the testApi from javascript
<code>
 TestApi.asyncCall("method", new Array("method", "args"), "testapi_cb", "callback args");
</code>
3. Define the TestApi from the Qt/C++
I can add the TestApi.asyncCall with addToJavaScriptWindowObject or 
QtScript, but i do not known how to call testapi_cb with return
results.
<code>
QVariant TestApi::jsCall(const QString &function, const QString &argument)
{
        QString script = QString( "%1.apply(this, %2)" ).arg(function).arg( argument );
        return  webframe->evaluateJavaScript(script);
}

int
TestApi::asyncCall(const QString& method_name,
                      const QList<QVariant> args,
                      const QString& callback,
                      const QString& user_data)
{
    //handle metdod
    //QVariant result = ..;
    jsCall(callback, user_data);
    return 0;
}
</code>

My point is how to pass the result to javascript? Thanks in advance.

Br/JL

-- 
Joshua Lee
Alpha Networks(Chengdu) Co., LTD
Shanghai Branch





More information about the Qt-interest-old mailing list