Results 1 to 4 of 4

Thread: How to send an object to Javascript function using QScriptEngine

  1. #1
    Join Date
    Oct 2008
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default How to send an object to Javascript function using QScriptEngine

    Hi,

    I have a question about QScriptEngine, how to pass an object from QT to Javascript function.
    And in Javascript i should be able to call the C++ function.

    Ex:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "QtScript/qscriptengine.h"
    3. #include <QFile>
    4.  
    5. class Sample: public QObject
    6. {
    7. public:
    8. int data;
    9. Sample(){
    10. data = 100;
    11. }
    12. int display()
    13. {
    14. return 100;
    15. }
    16. };
    17.  
    18. int main(int argc, char *argv[])
    19. {
    20. QApplication a(argc, argv);
    21.  
    22. QScriptEngine engine;
    23. QFile scriptFile("C:\\Users\\paruchsr\\Desktop\\Test\\test.js");
    24. scriptFile.open(QIODevice::ReadOnly);
    25. QString str =scriptFile.readAll();
    26. engine.evaluate(str);
    27. scriptFile.close();
    28.  
    29. //Sample *obj = new Sample();
    30. QObject *obj = new Sample();
    31.  
    32. QScriptValue ctor = engine.evaluate("Add");
    33. qint16 val = ctor.toInteger();
    34. QString str1 = ctor.toString();
    35.  
    36. QScriptValue scriptUi = engine.newQObject(obj, QScriptEngine::ScriptOwnership);
    37. str = scriptUi.toString();
    38. QScriptValue calc = ctor.construct(QScriptValueList() << scriptUi);
    39. str = calc.toString();
    40.  
    41. // obj->show();
    42.  
    43. return a.exec();
    44. }
    To copy to clipboard, switch view to plain text mode 

    Javascript Function:
    ==============
    Qt Code:
    1. function Add(test)
    2. {
    3. var a = test.display();
    4. return a;
    5. }
    To copy to clipboard, switch view to plain text mode 


    I could able to return some data from javascript function without using an object.But my requirement is to pass an object to javascript function and able to call function in javascript itself.

    Actually there was an example of Calculator, i have followed similarly. Here i don't hany any user interface. I am susupecting that the problem with newQObject funtion. I am feeling that newQObject function is not constructing an object properly like in Calculator.

    Thanks in Advance and i really appreciate your help.

    Regards
    Srinivas
    Last edited by marcel; 14th October 2008 at 19:31.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to send an object to Javascript function using QScriptEngine

    I'm not sure if I understand correctly what you're trying to do, but take a look at these paragraphs: http://doc.trolltech.com/4.3/qtscrip...n-in-qt-script .

    If you want a method of your object to be available in the script then you need to declare it with Q_INVOKABLE, just as in the example.

  3. The following user says thank you to marcel for this useful post:

    parusri (14th October 2008)

  4. #3
    Join Date
    Oct 2008
    Posts
    5
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Smile Re: How to send an object to Javascript function using QScriptEngine

    Thanks for the information.

    But the class member functions should be available in the javascript functions like

    ex:

    Qt Code:
    1. function Add(myObj)
    2. {
    3. myObj.display();
    4. }
    To copy to clipboard, switch view to plain text mode 
    In the above myObj is the C++ object passed from QScriptEngine.
    ex:
    Qt Code:
    1. QObject *obj = new Sample();
    2.  
    3. QScriptValue ctor = engine.evaluate("Add");
    4. qint16 val = ctor.toInteger();
    5. QString str1 = ctor.toString();
    6.  
    7. QScriptValue scriptUi = engine.newQObject(obj, QScriptEngine::ScriptOwnership);
    8. str = scriptUi.toString();
    9. QScriptValue calc = ctor.construct(QScriptValueList() << scriptUi);
    10. str = calc.toString();
    To copy to clipboard, switch view to plain text mode 


    C++ class:
    =======
    Qt Code:
    1. class Sample: public QObject
    2. {
    3. public:
    4. int data;
    5. Sample(){
    6. data = 100;
    7. }
    8. int display()
    9. {
    10. return data;
    11. }
    12. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by marcel; 14th October 2008 at 21:57. Reason: missing [code] tags

  5. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to send an object to Javascript function using QScriptEngine

    Yes, and in order to make a member function available in QtScript, you must either declare it as a slot or with Q_INVOKABLE in your QObject subclass.
    Something like this:

    Qt Code:
    1. class Sample: public QObject
    2. {
    3.  
    4. Q_OBJECT
    5.  
    6. public:
    7. int data;
    8. Sample(){
    9. data = 100;
    10. }
    11. Q_INVOKABLE int display()
    12. {
    13. return data;
    14. }
    15.  
    16. public slots:
    17. int display2()
    18. {
    19. return data;
    20. }
    21. };
    To copy to clipboard, switch view to plain text mode 

    Now you should be able to call both display and display2 in a script.

Similar Threads

  1. QPSQL problem
    By LoneWolf in forum Installation and Deployment
    Replies: 60
    Last Post: 4th November 2009, 15:22
  2. QPSQL driver in windows
    By brevleq in forum Installation and Deployment
    Replies: 31
    Last Post: 14th December 2007, 13:57
  3. how to add static library into qmake
    By Namrata in forum Qt Tools
    Replies: 1
    Last Post: 20th November 2007, 18:33
  4. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 07:13
  5. use qpsql
    By raphaelf in forum Installation and Deployment
    Replies: 34
    Last Post: 22nd August 2006, 13:52

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.