October 15, 2010

kyrie kyrie
Lab Rat
1 posts

JavaScript types in C++

 

Hi all!

I have a number of JavaScript events (like MouseMoved, etc.), which brings as parameter some JavaScript type (like JSPoint, for example). I want to emit C++ signals from JavaScript code, so I install object using addToJavaScriptWindowObject() with a number of signals, and then simply call them in js code. Everything works fine for build-in types like QString.
Question: how can I instruct Qt to perform auto-conversion for the signal arguments types? For example, I want to create C++ type CPoint, and I want Qt to convert JSPoint to CPoint when I emit signal.
I think that this problem may be solved using QMetaType – register new type – but what else? I found articles in documentation, but they tell how to make C++ object accessible in script envirinment (js or Qt script).

Any ideas?

4 replies

October 15, 2010

danilocesar danilocesar
Lab Rat
161 posts

Can you transform them to json objects?

They will be recognized by c++ as QVariantMap.

 Signature 

Danilo Cesar Lemes de Paula
Software Engineer

October 15, 2010

Benjamin Poulain Benjamin Poulain
Lab Rat
153 posts

For example, I want to create C++ type CPoint, and I want Qt to convert JSPoint to CPoint when I emit signal.

This is not possible. Think about it, how would we implement that?

October 15, 2010

kyrie kyrie
Lab Rat
1 posts

As ‘fixme’ solution I use conversion to string before emit, then emit QString, and then decode data from string. I am lucky that all JavaScript types what I need have ‘toString()’ method.
Conversion to json objects is not suitable, but I knew nothing about this variant, thanks a lot.
Concerning the possible implementation, quick dirty idea is to allow conversion to those types which are registered in Qt metatype system. Valid type is selected according to name comparison. The same way we select qt property in C++ object correspondent to js object field: field name comparison. But before generating ideas -) , it’s better to find how Qt now supports conversion between some build-in types, for example – QString – I don’t know how it was done.

Thanks to all.

October 15, 2010

Benjamin Poulain Benjamin Poulain
Lab Rat
153 posts

it’s better to find how Qt now supports conversion between some build-in types, for example – QString – I don’t know how it was done.

Here is the code:
http://gitorious.org/webkit/qtwebkit/blobs/qtwebkit-2.1/WebCore/bridge/qt/qt_runtime.cpp [gitorious.org]

 
  ‹‹ Flash is working too slow using QGraphicsWebView      [Solved] QtWebKit Bridge ››

You must log in to post a reply. Not a member yet? Register here!