[webkit-qt] Adding Javascript Bindings

Michael Ditum mike at mikeditum.co.uk
Thu Dec 16 16:20:16 PST 2010

On Thu, Dec 16, 2010 at 7:09 PM, Benjamin Poulain <
benjamin.poulain at nokia.com> wrote:

> 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?
> The solution commonly used is to do a signal-"slot" connection between a
> signal from Qt and a Javascript function:
> someObject.youSignal.connect(jsFunction);
>  2. Is it possible to return an object from a javascript call that
>> behaves like an object added with addToJavaScriptWindowObject ?
> 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.


Thanks for the quick reply. Both of those suggestions worked perfectly!

I didn't realise you could connect to signals from javascript, the
documentation for addToJavaScriptWindowObject only lists properties and
slots. It would probably be a good idea to get the documentation updated,
should I raise this on http://bugreports.qt.nokia.com or somewhere else?

As for returning an object to javascript I ended up using the following code
which worked perfectly...

QVariant JSBinding::GetStuff()
    QVariant var(QMetaType::QObjectStar);
    return var;

Where item is a pointer to a class extending QObject with properties,
signals and slots. So the two things I was missing was the specifying of the
Variant type and casting the item I wanted to return to a QObject pointer.

I'm hoping to attempt a blog post with everything I've learnt in the next
couple of days as there are plenty with how to use
addToJavaScriptWindowObject for properties and simple slots but I couldn't
find any for the questions I had (at least there aren't any using the terms
I was googling).

Again, thanks a lot!

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-qt/attachments/20101217/fed904af/attachment.html>

More information about the webkit-qt mailing list