Results 1 to 8 of 8

Thread: Using QString in evaluateJavaScript as agrument

  1. #1
    Join Date
    Jun 2010
    Posts
    6
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Symbian S60

    Default Using QString in evaluateJavaScript as agrument

    Hello,

    I need to send a string which contains newline to javascript function as an argument.

    Qt Code:
    1. Example:
    2. QString sampleString = "first line \n second line.";
    3.  
    4. const char *kModifyString="\
    5. function modifyString(str)\
    6. {\
    7. return str;\
    8. }\
    9. modifyString(\"%1\");";
    10.  
    11. webFrame->evaluateJavaScript(QString(kModifyString).arg(sampleString ));
    To copy to clipboard, switch view to plain text mode 
    But %1 do not take QString with newline.

    How can I handle this situation? I need to send sampleString to javascript function as argument.

    Thanks,

  2. #2
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QString in evaluateJavaScript as agrument

    I had modified your code a little like this:
    Qt Code:
    1. QString sampleString = "first \n second";
    2.  
    3. const char *kModifyString="123 %1 456";
    4.  
    5. QString s = QString( kModifyString ).arg( sampleString );
    6. char xx[ 1000 ];
    7. strcpy( xx, s.toAscii().constData() );
    To copy to clipboard, switch view to plain text mode 
    and here is a result:

    you see, that xx[ 10 ] equals 10, that is a new line

  3. #3
    Join Date
    Jun 2010
    Posts
    6
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Symbian S60

    Default Re: Using QString in evaluateJavaScript as agrument

    Hello,
    Thanks for snippet. The .arg takes string with newline.
    But the evaluateJavaScript needs it to specify as \\n instead of \n. But I am unaware of what all the characters my string can contain.
    So I want to escape the newline and similiar characters. Any help regarding this.

  4. #4
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QString in evaluateJavaScript as agrument

    You can escape the string, passing to eval function like this
    Qt Code:
    1. QString scriptString = QString(kModifyString).arg(sampleString);
    2. scriptString.replace( "\n", "\\n" );
    3. webFrame->evaluateJavaScript( scriptString );
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using QString in evaluateJavaScript as agrument

    You will also have to do:
    \r
    \"
    \\

  6. #6
    Join Date
    Apr 2010
    Location
    Rostov-na-Donu, Russia
    Posts
    153
    Thanks
    2
    Thanked 26 Times in 23 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QString in evaluateJavaScript as agrument

    By the way (sorry for using alient thread to ask) is there any C++ libraries, provided standard (un)escape functions?
    I needed (right form of need in past? ) it to save/load some not simple strings in ini-file.

  7. #7
    Join Date
    Jul 2009
    Posts
    139
    Thanks
    13
    Thanked 59 Times in 52 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Using QString in evaluateJavaScript as agrument

    Qt Code:
    1. QString escapeJavascriptString(const QString & str)
    2. {
    3. QString out;
    4. QRegExp rx("(\\r|\\n|\\\\|\")");
    5. int pos = 0, lastPos = 0;
    6.  
    7. while ((pos = rx.indexIn(str, pos)) != -1)
    8. {
    9. out += str.mid(lastPos, pos - lastPos);
    10.  
    11. switch (rx.cap(1).at(0).unicode())
    12. {
    13. case '\r':
    14. out += "\\r";
    15. break;
    16. case '\n':
    17. out += "\\n";
    18. break;
    19. case '"':
    20. out += "\\\"";
    21. break;
    22. case '\\':
    23. out += "\\\\";
    24. break;
    25. }
    26. pos++;
    27. lastPos = pos;
    28. }
    29. out += str.mid(lastPos);
    30. return out;
    31. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jun 2010
    Posts
    6
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Symbian S60

    Default Re: Using QString in evaluateJavaScript as agrument

    Thanks for everyone.
    It worked.
    First, I need to escape with \\ and then others.

Similar Threads

  1. QWebElement::evaluateJavaScript strange behaviour
    By piotr.dobrogost in forum Qt Programming
    Replies: 3
    Last Post: 11th January 2011, 11:45
  2. Replies: 2
    Last Post: 16th June 2010, 15:42
  3. QWebFrame evaluateJavaScript
    By bunjee in forum Qt Programming
    Replies: 2
    Last Post: 2nd September 2009, 07:55
  4. Problem with evaluateJavaScript()
    By piotr.dobrogost in forum Qt Programming
    Replies: 0
    Last Post: 26th August 2009, 19:36

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.