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

Garth Dahlstrom ironstorm at gmail.com
Tue Dec 14 02:45:33 CET 2010


Hi Joshua,

I can't answer your specific question, but if you are interested in working with Javascript calls back and forth between C++ and JavaScript,  I would point you at the DJ software I used to work on called Mixxx (you can find the source on launchpad.net)

Mixxx implements pretty extensive Javascript<->C++ as a means for users to write customized control behaviour for MIDI controllers (DJ hardware: turntables, samplers, faders, etc)... 

Mixxx/src/script/scriptingengine.cpp and related files are probably good to look at as a good non-trivial working reference.

Cheers,

-G

On 2010-12-13, at 5:40 AM, qt-interest-request at trolltech.com wrote:

> Date: Mon, 13 Dec 2010 18:02:04 +0800
> From: Joshua Lee <muzili at gmail.com>
> Subject: [Qt-interest] How to calling javascript callback from Qt/C++
> To: Qt-interest <qt-interest at trolltech.com>
> Message-ID: <20101213100204.GA9512 at josh-dev>
> Content-Type: text/plain; charset=utf-8
> 
> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20101213/5eae5164/attachment.html 


More information about the Qt-interest-old mailing list