Results 1 to 4 of 4

Thread: Pass data from QT to javascript function

  1. #1
    Join Date
    Feb 2011
    Posts
    6
    Qt products
    Qt/Embedded

    Default Pass data from QT to javascript function

    I have a question that i really don't know.
    Here is the my code.
    Qt Code:
    1. QString from;
    2. QFile file( ":/direction.js");
    3. if ( !file.open(QIODevice::ReadOnly | QIODevice::Text) )
    4. {
    5. msg.setText("Fail");
    6. msg.exec();
    7. }
    8. else
    9. {
    10. from = "6";
    11. QString functions = file.readAll();
    12. QWebFrame* frame = ui->webView->page()->mainFrame();
    13. frame->evaluateJavaScript( functions );
    14.  
    15. QString script = QString("Direction(%1);").arg( from );
    16. frame->evaluateJavaScript(script);
    17. }
    To copy to clipboard, switch view to plain text mode 
    and the content of direction.js like :
    Qt Code:
    1. function Direction (from)
    2.  
    3. {
    4.  
    5. alert("This is Direction");
    6. alert(from);
    7.  
    8. }
    To copy to clipboard, switch view to plain text mode 

    But i am very confused with the value of QString from.
    when i put it : from="8".It run OK
    But when i put it : from="a".It can not run.
    If you know this problem.Please explain it for me.
    Thanks and best regards!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Pass data from QT to javascript function

    I am not a javascript guy but isn't
    javascript Code:
    1. Direction(3);
    To copy to clipboard, switch view to plain text mode 
    correct while
    javascript Code:
    1. Direction(a);
    To copy to clipboard, switch view to plain text mode 
    needs quotation marks?

  3. #3
    Join Date
    Feb 2011
    Posts
    6
    Qt products
    Qt/Embedded

    Default Re: Pass data from QT to javascript function

    Thanks very much. I have to add in QT as follow :
    from = "\""+myString+"\"";

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Pass data from QT to javascript function

    Also not sure again, but simple quotations marks should be allowed as well. Thus there is no need to escape the quotation marks which makes the code more readable (in my opinion):
    Qt Code:
    1. from = QString("'%1'").arg(myString);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 13
    Last Post: 30th November 2010, 13:47
  2. Replies: 2
    Last Post: 15th November 2010, 06:17
  3. Replies: 14
    Last Post: 1st December 2009, 21:45
  4. Replies: 3
    Last Post: 14th October 2008, 22:04
  5. How to pass a QComboBox to a function?
    By Ricardo_arg in forum General Programming
    Replies: 4
    Last Post: 9th March 2008, 23:16

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.