Problems in Implementing Save Feature in Javascript app using Qt
-
I am not sure to understand your problem. Can't you just have '\n' in your QString?
-
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...
-
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 .
-
QFile::readAll()?
-
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 ? -
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++).
-
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
-
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.