Results 1 to 6 of 6

Thread: How to pass an argument to evaluateJavaScript function?

  1. #1
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default How to pass an argument to evaluateJavaScript function?

    Qt Code:
    1. for (int i = 0; i < centerPointsList.size (); i++)
    2. {
    3. QVariant holdInformation = map->page ()->mainFrame ()->evaluateJavaScript (QString ("constructFileName (%1).arg (centerPointsList[0].toFloat())"));
    4. QList <QVariant> allListObj = holdInformation.toList ();
    5. QList <QVariant> fileNamesList = allListObj[0].toList ();
    6.  
    7. std :: cout << fileNamesList[0].toFloat() << "================= \n";
    8.  
    9. }
    To copy to clipboard, switch view to plain text mode 

    This results in:
    "SyntaxError: Parse error on line:1 Source:undefined"
    Segmentation fault
    I am guessing that the error is in the way I am passing the list item to the function evaluateJavaScript.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to pass an argument to evaluateJavaScript function?

    See if this works

    Qt Code:
    1. QString ("constructFileName (%1).arg (centerPointsList[0].toFloat())"); //Change this as below
    2. QString ("constructFileName (%1)").arg(centerPointsList[0].toFloat());
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

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

    TheIndependentAquarius (5th January 2013)

  4. #3
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: How to pass an argument to evaluateJavaScript function?

    I tried your way like this:
    Qt Code:
    1. for (int i = 0; i < centerPointsList.size (); i++)
    2. {
    3. QVariant holdInformation = map->page ()->mainFrame ()->evaluateJavaScript (QString ("constructFileName (%1)").arg (centerPointsList [0].toFloat ()));
    To copy to clipboard, switch view to plain text mode 

    which resulted in:
    "TypeError: Result of expression 'centerPointFileName.split' [undefined] is not a function. on line:65 Source:file:///..../index.html"
    The function constructFileName (in Javascript) is as follows:
    Qt Code:
    1. function constructFileName (centerPointFileName)
    2. {
    3. var removeSpaces = centerPointFileName.split (" ");
    4. var fileNameWithoutSpaces = "", i;
    5. for (i = 0; i < removeSpaces.length; i++)
    6. fileNameWithoutSpaces = fileNameWithoutSpaces + removeSpaces [i];
    To copy to clipboard, switch view to plain text mode 
    Last edited by TheIndependentAquarius; 5th January 2013 at 08:19.

  5. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: How to pass an argument to evaluateJavaScript function?

    If I am not mistaken the error is inside Java script, not in Qt Code.

    Further the error says "TypeError", you are passing a float as argument to JS, where as it expects centerPointFileName (whatever type it is), a spilt on it is called, not sure what will a spilt on float do. (that too split with space).

    I guess JS is expecting a multi word string.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #5
    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: How to pass an argument to evaluateJavaScript function?

    Float does not have a "split" method, only String does.
    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. #6
    Join Date
    Apr 2011
    Posts
    231
    Thanks
    141
    Thanked 6 Times in 5 Posts

    Default Re: How to pass an argument to evaluateJavaScript function?

    I cross posted this on stackoverflow and the solution presented in the below link solved my error.
    http://stackoverflow.com/a/14170076/462608

Similar Threads

  1. Function: ... Argument
    By sonulohani in forum General Programming
    Replies: 3
    Last Post: 15th December 2012, 15:55
  2. evaluateJavaScript function can be used for loading maps in qt widgets?
    By TheIndependentAquarius in forum Qt Programming
    Replies: 14
    Last Post: 27th May 2011, 10:29
  3. Replies: 3
    Last Post: 21st May 2011, 14:58
  4. Replies: 5
    Last Post: 20th September 2010, 10:30
  5. Replies: 3
    Last Post: 28th February 2010, 08:10

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.