PDA

View Full Version : How to return a Javascript object to Javascript environment?



cysin
27th November 2010, 14:11
I am currently working on this topic: http://doc.qt.nokia.com/4.7-snapshot/qtwebkit-bridge.html

So I am wondering how to return a JS object to JS environment? Is there an example?

wysota
27th November 2010, 16:28
Would you care to elaborate?

cysin
28th November 2010, 02:08
what you mean elaborate? I need some details about how to return an object, better with an example

wysota
28th November 2010, 02:20
The description of your problem is unclear. Elaborate on the problem please.

cysin
28th November 2010, 03:21
Sorry my English is not that good.

I am currently adding an object like this


ui->webView->page()->mainFrame()->addToJavaScriptWindowObject(QString("myjsobj"), myjsobj)

So I can access C++ class from Javascript environment. And also I want this class could return an object.

Let's say, the C++ class has a function called "getItem()", so I can access this function using Javascript:


var item = myjsobj.getItem();
alert(item.getName());
alert(item.getPrice());


I want "getItem()" function to return another object, this object might be dynamically generated.

Thank you~

wysota
28th November 2010, 10:05
Ok but what is it exactly that you tried and didn't work? I still fail to see the problem...

cysin
29th November 2010, 03:48
Ok but what is it exactly that you tried and didn't work? I still fail to see the problem...


QObjectList pcp::getObjectArray() {
js_pqm* pqm1 = new js_pqm(w);
pqm1->setObjectName(QString("iampqm1"));
js_pqm* pqm2 = new js_pqm(w);
pqm2->setObjectName(QString("iampqm2"));
QObjectList objArray;
objArray.push_back(pqm1);
objArray.push_back(pqm2);
return objArray;
}

This is a function of the C++ class that maps to javascript array. I want this function to return a javascript array that contains javascript object. but it failed to recongnize the returned 'QObjectList', said "TypeError: cannot call getObjectArray(): unknown type `QObjectList' on line:35

So I am wondering what Qt type this function should return to make JS engine recognize them correctly

thanks

wysota
29th November 2010, 10:34
There is an automatic mapping scheme between Qt/C++ types and javascript types. See the page you quoted in your first post for details.

cysin
29th November 2010, 11:22
There is an automatic mapping scheme between Qt/C++ types and javascript types. See the page you quoted in your first post for details.

But, what's wrong with my posted code? It should return an array contains objects

wysota
29th November 2010, 13:12
It's likely that something is not registered somewhere. It's really hard to say anything useful if you just keep sending unrelated pieces of code. Provide a minimal compilable example reproducing the problem.