Problems in Implementing Save Feature in Javascript app using Qt
-
wrote on 26 Sept 2010, 09:16 last edited by
Also, I too worked with a very simple javascript code, passing on the variables and it worked fine. But when it comes to this app, I am facing above problems.
-
wrote on 9 Oct 2010, 13:53 last edited by
Hi,
I was trying to sort out the problems. Actually, the string which I need to pass to JavaScript function is multi-line string.Is there any method in Qt Programming to pass multi-line string ?
-
wrote on 9 Oct 2010, 15:46 last edited by
I am not sure to understand your problem. Can't you just have '\n' in your QString?
-
wrote on 9 Oct 2010, 16:31 last edited by
Didn't we have this discussion before in "this thread":http://developer.qt.nokia.com/forums/viewthread/523 ? Unfortunately that thread seems to no longer be available...
-
wrote on 9 Oct 2010, 17:22 last edited by
Yes, we had a discussion but at that time , you wrote a long post giving me instructions right from scratch. So I started working on it again. I was committing blunders at that time.
Now, I have made various changes in Qt code. I have tested this on a demo javascript application as well and its working fine . That is using this code, I was able to pass my QString (which I read from file) to JavaScript function and display it as well. Though this particular QString was short of length (actually I used mid() function to get a part of it). Short length QString worked fine.
But, for my actual JavaScript app, I need to pass whole multi-line string, which I am not able to. I can save it , so my save feature is done . But to open it, I need to pass this multi-line string. I need a solution for this .
-
wrote on 10 Oct 2010, 07:59 last edited by
QFile::readAll()?
-
wrote on 10 Oct 2010, 13:46 last edited by
Hi
I can read the contents of saved file.
@QDataStream in(&file);
in.setVersion(QDataStream::Qt_4_5);
//contacts.empty(); // empty existing contact
in >> output;
qDebug() << output;@I get all the contents of file in QString variable output. I can see that in terminal and contents are correct. But I am not able to pass this multi-line QString variable to JavaScript function.
How can QFile::readAll() help me in this . I am really sorry, but how should I implement it in my code ? -
wrote on 10 Oct 2010, 14:41 last edited by
I do not know javascript, but assuming output is "Line 1\nLine 2\n" you are asking the interpreter to handle this text:
@
open('Line 1
Line 2
');
@I would be surprised if a Javascript interpreter would handle that.
You will need to escape the line breaks. I do not know enough javascript to tell you how though:-) Basically you need to make sure that what the javascript interpreter gets does contain the line breaks (and any other special characters!) in the format javascript expects them to be escaped in its strings. Maybe replacing '\n' (note that this is actually one character in C/C++) with "\n" (note that these are two characters in C/C++).
-
wrote on 11 Oct 2010, 02:47 last edited by
Tobias is right, in the "example":http://developer.qt.nokia.com/wiki/QString_variable_to_Javascript you can replace this line
@
QString data("Qt is the Best!");
@with this
@
QString data("Qt is the Best!\nQt Rocks!!");
@and you will get line break in your alert
-
wrote on 24 Oct 2011, 05:55 last edited by
I see this is a very old post but would have liked this solution shown for me when I found this post. JavaScript does has problems taking string with line breaks in it. In my case (read kml file stored on local hard drive, pass the string to JavaScript, and use the Google Earth extensions library to parse and display the kml file) the line breaks did not need to be conserved in a file load. To solve this I used:
@
QTextStream in(&file);
QString wholeFile;
QString line = in.readLine();
wholeFile+=line;
while (!line.isNull()) {
line = in.readLine();
wholeFile+=line;
}
@
An alternative method:
@
QByteArray line = file.readAll();
@
does not play well with JavaScript.