[webkit-qt] Adding Javascript Bindings

Benjamin Poulain benjamin.poulain at nokia.com
Thu Dec 16 11:09:56 PST 2010

On 12/16/2010 07:03 PM, ext Michael Ditum wrote:
> 1. What's the best way of handling adding event listeners and raising
> events with my javascript API?
> Currently I'm using the following syntax to assign an event listener...
> JSBinding.onEvent = "CallFunction()";
> and then I use QWebFrame's evaluateJavaScript to raise the event. This
> has a couple of down sides; Only one event listener can be registered at
> once, it's not easy to pass parameters into the function and it's
> handled differently to adding normal js event listeners. Is there any
> way to be able to pass a javascript function in so I can store that and
> then call it later on with the parameters I need? I've tried specifying
> the function's parameter as a QVariant but the function just seems to be
> provided as a VariantMap with 0 items.

The solution commonly used is to do a signal-"slot" connection between a 
signal from Qt and a Javascript function:


> 2. Is it possible to return an object from a javascript call that
> behaves like an object added with addToJavaScriptWindowObject ?
> I can easily returns ints, strings, lists of strings and integers, etc.
> I've also been able to return the equivalent of a JSONified object (i.e.
> just data) by returning it as a QMap<QString, QVariant>, however what I
> would like to do is is return an object that has properties and methods
> (through slots) so the javascript can interact with my c++ objects
> directly. If I try to return an object I get a compiler error saying
> that QObject's copy constructor is private, if I return a pointer to an
> Object I get an empty string returned and if I try and return the object
> inside a QVariant I again get copy constructor compiler errors. I don't
> know if I'm doing something incorrectly to cause these compiler errors
> or if it's just not currently possible.
> Both of these issues I can easily work around (evaling code for 1 and
> providing a procedural style API for 2) so if these things are not
> possible it's not a huge deal. I just wanted to make sure I hadn't
> missed anything before I set my Javascript API in stone.

Passing a QObject by value cannot work.

 From a quicklook at convertQVariantToValue(), I think passing a pointer 
should work. A QVariant with with the type QMetaType::QObjectStar should 
give you a nice conversion.

I suggest you to set a breakpoint in convertQVariantToValue 
(qt_runtime.cpp) to figure what is happening in your app.


More information about the webkit-qt mailing list