Problems in Implementing Save Feature in Javascript app using Qt
-
Your understanding is absolutely correct.
-
When I save the file in my system that is in Ubuntu 10.04 and open it in say notepad, I can see the content.
-
But when I access the same file via Maemo platform through my application, I dont see my content. I can see that content in terminal since I used qDebug but not in my application or rather in the webview created.
-
-
so,
@
qDebug() << output; //this works, means output has the file contents
// but next line fails
webView->page()->mainFrame()->evaluateJavaScript(QString("open('%1')").arg(output));
@In "my example":http://developer.qt.nokia.com/wiki/QString_variable_to_Javascript passing QString worked fine. Can you use an alert dialog within the javascript and confirm the value is being passed properly? Can you post your javascript function here?
@
function open(str)
{
alert(str);
... your code here
} -
I saw your code. My javascript app is sort of spreadsheet and GUI I designed for this using Qt Creator contains 3 buttons :
-
Start : Which loads the spreadsheet.
-
Save : Which saves the content.
3 . Open : Which opens the saved file.
Code for Myapp.cpp besides two functions I already posted has :
@Test::Test(QWidget *parent):QWidget(parent)
{
setupUi(this);
}void Test ::on_pushButton1_clicked()
{webView->setUrl(QUrl("index.html"));
}@
open() function in which string is to passed so that complete spreadsheet is built along with content displayed on it is :
@function open(str) {
var parts = spreadsheet.DecodeSpreadsheetSave(str);
alert("hello");
if (parts) {
if (parts.sheet) { spreadsheet.sheet.ResetSheet(); spreadsheet.ParseSheetSave(str.substring(parts.sheet.start, parts.sheet.end)); } if (parts.edit) { spreadsheet.editor.LoadEditorSettings(str.substring(parts.edit.start, parts.edit.end)); } }
if (spreadsheet.editor.context.sheetobj.attribs.recalc=="off") {
spreadsheet.ExecuteCommand('redisplay', ''); }
else {
spreadsheet.ExecuteCommand('recalc', ''); }
}
@
Its a complex code since its associated with various other function defined.
I used alert("hello") in the code and run my app on the system rather than on maemo platform. When I open a certain file ,Alert box appears after I close the open dialogue box without selecting any file to open.
Thats because QString "output" (which contains something random) gets passed to JavaScript function and hence the dialogue box appears. -
-
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.