Results 1 to 10 of 10

Thread: How to return a Javascript object to Javascript environment?

  1. #1
    Join Date
    Nov 2010
    Posts
    5

    Default How to return a Javascript object to Javascript environment?

    I am currently working on this topic: http://doc.qt.nokia.com/4.7-snapshot...it-bridge.html

    So I am wondering how to return a JS object to JS environment? Is there an example?
    Last edited by cysin; 27th November 2010 at 14:14. Reason: typo

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to return a Javascript object to Javascript environment?

    Would you care to elaborate?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2010
    Posts
    5

    Default Re: How to return a Javascript object to Javascript environment?

    what you mean elaborate? I need some details about how to return an object, better with an example

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to return a Javascript object to Javascript environment?

    The description of your problem is unclear. Elaborate on the problem please.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2010
    Posts
    5

    Default Re: How to return a Javascript object to Javascript environment?

    Sorry my English is not that good.

    I am currently adding an object like this

    Qt Code:
    1. ui->webView->page()->mainFrame()->addToJavaScriptWindowObject(QString("myjsobj"), myjsobj)
    To copy to clipboard, switch view to plain text mode 

    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:

    Qt Code:
    1. var item = myjsobj.getItem();
    2. alert(item.getName());
    3. alert(item.getPrice());
    To copy to clipboard, switch view to plain text mode 

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

    Thank you~
    Last edited by cysin; 28th November 2010 at 03:22. Reason: typo

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to return a Javascript object to Javascript environment?

    Ok but what is it exactly that you tried and didn't work? I still fail to see the problem...
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Nov 2010
    Posts
    5

    Default Re: How to return a Javascript object to Javascript environment?

    Quote Originally Posted by wysota View Post
    Ok but what is it exactly that you tried and didn't work? I still fail to see the problem...
    Qt Code:
    1. QObjectList pcp::getObjectArray() {
    2. js_pqm* pqm1 = new js_pqm(w);
    3. pqm1->setObjectName(QString("iampqm1"));
    4. js_pqm* pqm2 = new js_pqm(w);
    5. pqm2->setObjectName(QString("iampqm2"));
    6. QObjectList objArray;
    7. objArray.push_back(pqm1);
    8. objArray.push_back(pqm2);
    9. return objArray;
    10. }
    To copy to clipboard, switch view to plain text mode 

    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

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to return a Javascript object to Javascript environment?

    There is an automatic mapping scheme between Qt/C++ types and javascript types. See the page you quoted in your first post for details.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. #9
    Join Date
    Nov 2010
    Posts
    5

    Default Re: How to return a Javascript object to Javascript environment?

    Quote Originally Posted by wysota View Post
    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

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to return a Javascript object to Javascript environment?

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Mapping JavaScript object to QT (and vice versa)
    By leoalvesmachado in forum Newbie
    Replies: 2
    Last Post: 30th June 2010, 20:00
  2. javascript problem
    By bmn in forum Qt Programming
    Replies: 1
    Last Post: 24th August 2009, 14:40
  3. Replies: 3
    Last Post: 14th October 2008, 22:04
  4. Javascript
    By ale301168 in forum Qt Programming
    Replies: 0
    Last Post: 7th September 2008, 00:23
  5. JavaScript in QTextBrowser
    By manojmka in forum Qt Programming
    Replies: 4
    Last Post: 27th December 2007, 09:22

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.