Results 1 to 9 of 9

Thread: Qt script combined with java script

  1. #1
    Join Date
    Apr 2015
    Posts
    26
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded PyQt3 PyQt4
    Platforms
    Windows

    Default Qt script combined with java script

    hi,
    anyone knows how to pass Qt classes to java script through qt script engine?

    I am writing a Qt application where i have used Qt Script engine to evaluate java script.and to run functions from inside java script.
    in java script i want to create a tcp client socket. Is it possible to pass Qtcpsocket class to java script through qt script engine in any way ? or is there any other way to accomplish this task?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt script combined with java script


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

    prachi kamble (16th April 2015)

  4. #3
    Join Date
    Apr 2015
    Posts
    26
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded PyQt3 PyQt4
    Platforms
    Windows

    Default Re: Qt script combined with java script

    will i be able to use all the methods of QTcpsocket class from within the js file(java script file).


    Added after 25 minutes:

    Qt Code:
    1. QString scriptFileName("./AnotherJs.js");
    2. QFile scriptFile(scriptFileName);
    3. scriptFile.open(QIODevice::ReadOnly);
    4. engine.evaluate(scriptFile.readAll(), scriptFileName);
    5. bool b1 = engine.hasUncaughtException();
    6. scriptFile.close();
    7.  
    8. ctor = engine.evaluate("on_jsfile_clicked");
    9. b1 = engine.hasUncaughtException();
    10. scriptUi = engine.newQObject(&socket, QScriptEngine::ScriptOwnership);
    11. b1 = engine.hasUncaughtException();
    12.  
    13. //QMetaObject *objmeta = new QMetaObject;
    14. //QTcpSocket objsocket;
    15. //objmeta->cast(&objsocket);
    16. //engine.newQMetaObject(objmeta,ctor);
    17.  
    18. tcpsocket = engine.scriptValueFromQMetaObject<QTcpSocket>();
    19. engine.globalObject().setProperty("QTcpSocket", tcpsocket);
    20.  
    21. calc = ctor.construct(QScriptValueList() << scriptUi); // this line gives error,why this is so? (error in the sense engine.hasUncaughtException() returns true value).
    22. b1 = engine.hasUncaughtException(); //and socket is not getting connected to server
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 16th April 2015 at 09:56. Reason: missing [code] tags

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt script combined with java script

    If you have an existing socket, then the newQObject() call is enough.
    The scriptValueFromQMetaObject() is only needed if the script wants to create QTcpSocket instances.

    Cheers,
    _

  6. #5
    Join Date
    Apr 2015
    Posts
    26
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded PyQt3 PyQt4
    Platforms
    Windows

    Default Re: Qt script combined with java script

    i have passed socket object as an orgument, below is the code
    scriptUi = engine.newQObject(&socket, QScriptEngine::ScriptOwnership);
    calc = ctor.construct(QScriptValueList() << scriptUi);

    but still, in "AnotherJs.js" file i m not able to execute following code

    // QTcpSocket objsocket;
    this = ui;
    this.connectToHost("10.7.15.20",50000);

    y this is happening? am i doing some wrong coading? please rply

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

    Default Re: Qt script combined with java script

    connectToHost is not a slot, it will not be visible from within a script. You would have to make it a slot first.
    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.


  8. #7
    Join Date
    Apr 2015
    Posts
    26
    Thanks
    2
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded PyQt3 PyQt4
    Platforms
    Windows

    Default Re: Qt script combined with java script

    connectToHost() is QTcpSocket's Function, won't it be available to the java script, as i am passing the class to java script while evaluating it.?

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

    Default Re: Qt script combined with java script

    Quote Originally Posted by prachi kamble View Post
    connectToHost() is QTcpSocket's Function, won't it be available to the java script, as i am passing the class to java script while evaluating it.?
    Yes, that's what I said, it won't be visible from within a script.
    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.


  10. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt script combined with java script

    Quote Originally Posted by prachi kamble View Post
    i have passed socket object as an orgument, below is the code
    Why pass a socket as a parent to the constructor of a new socket?

    Quote Originally Posted by prachi kamble View Post
    connectToHost() is QTcpSocket's Function, won't it be available to the java script, as i am passing the class to java script while evaluating it.?
    Only slots or functions marked as Q_INVOKABLE are accessible to the script.

    Cheers,
    _

  11. The following user says thank you to anda_skoa for this useful post:

    prachi kamble (17th April 2015)

Similar Threads

  1. Qml java script
    By ganeshgladish in forum Newbie
    Replies: 6
    Last Post: 7th June 2013, 17:52
  2. java script function
    By ganeshgladish in forum Newbie
    Replies: 1
    Last Post: 5th June 2013, 14:19
  3. Replies: 0
    Last Post: 4th August 2011, 12:37
  4. Auto login using java script
    By johnsoga in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2011, 12:06
  5. Java Script on Qt4.5
    By kavinsiva in forum Newbie
    Replies: 1
    Last Post: 7th October 2009, 14:08

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.