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?
-
Can you transform them to json objects?
They will be recognized by c++ as QVariantMap.
-
bq. 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?
-
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.
-
bq. 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":http://gitorious.org/webkit/qtwebkit/blobs/qtwebkit-2.1/WebCore/bridge/qt/qt_runtime.cpp