Problems in Implementing Save Feature in Javascript app using Qt
-
Hi,
I have an application written in JavaScript. I am trying to port it on on Maemo Platform using Qt.
I have defined dosave()function in my javascript file which saves the contents in a “string” and returns it.
Similarly, I have open() function defined in javascript code, in which this particular string is passed and it displays the content back to the file I want to open. I have used evaluateJavaScript() function to interact with the javascript function.
I can save the file but when I pass the string to open(), I dont see my content. How can I pass QString variable to javascript function in Qt programming ? Kindly suggest something.
Here is the code for opening the saved file :
@
void Test::on_pushButton3_clicked() /// This function is activated when I click open button
{
QString output;
QString fileName = QFileDialog::getOpenFileName(this);
qDebug() << fileName;
if (!fileName.isEmpty()) {
// read from file
QFile file(fileName);if (!file.open(QIODevice::ReadOnly)) { QMessageBox::information(this, tr("Unable to open file"), file.errorString()); return; } QDataStream in(&file); in.setVersion(QDataStream::Qt_4_5); in >> output; qDebug() << output; /// output is the string I need to send to JavaScript open() function. I checked it using qDebug and its //// working fine. webView->page()->mainFrame()->evaluateJavaScript(QString("open(\"%1\")").arg(output)); }
}
@Here is the code for saving the file :
@
void Test::on_pushButton2_clicked()
{
{
QVariant content = webView->page()->mainFrame()->evaluateJavaScript("dosave()");
QString data = content.toString();
qDebug() << data;
QString fileName = QFileDialog::getSaveFileName(this);if (!fileName.isEmpty()) { // save to file QFile file(fileName); if (!file.open(QIODevice::WriteOnly)) { QMessageBox::information(this, tr("Unable to open file"), file.errorString()); return; } QDataStream out(&file); out.setVersion(QDataStream::Qt_4_5); out << data; } }
}
@I have used qDebug at different places to check the data and its working fine. Kindly suggest ways to send QString variable to Javascript function so that content can be displayed.
[edit: fixed @tag / chetankjain]
-
Hi,
I implemented it as it is in on_pushButton3_clicked() function after the statement qDebug() << output;
But code did not compile and showed error : 'class QWebFrame' has no member named 'frame'.
Therefore I implemented it again after the same statement as follows :
@
QString filedata("This contains file data");
QString param = QString("open('%1')").arg(filedata);
QVariant temp = webView->page()->mainFrame()->evaluateJavaScript(param); /// since return type of evaluateJavaScript() is QVariant.
@But again when I open the saved file, I don't see any content in it. Did I implement it in wrong manner ?
-
[quote author="shukla" date="1285433755"]... But code did not compile and showed error : 'class QWebFrame' has no member named 'frame'.
...
But again when I open the saved file, I don't see any content in it. Did I implement it in wrong manner ?
[/quote]ok frame should've been assigned as follows:
@
QWebFrame *frame = webView->page()->mainFrame()
@I'm using the same code snip that you have posted, and my javascript function is returning a string, and when I save it to file, it just works fine! But have extra spaces in that file.
-
Still I am not able to see string filedata in being displayed.
I wrote the same after after the statement qDebug() << output;
@
QString filedata("This contains file data");
QString param = QString("open('%1')").arg(filedata);
QWebFrame *frame = webView->page()->mainFrame();
frame->evaluateJavaScript(param);
@I created new file, saved it but when I opened it , I could not see any content I wrote. That content was in output QString variable. I implemented it as :
@
QString filedata("This contains file data");
QString param = QString("open('%1')").arg(output);
QWebFrame *frame = webView->page()->mainFrame();
frame->evaluateJavaScript(param);
@
However, I did not succeed. -
I just edited my earlier post saying that I was even able to save to a file.
If you got the return value from your javascript and are able to display it using qDebug,
@
qDebug << data; // this works doesn't it?
@
then the issue is with the file I/O ... look at that part. -
let me again understand,
you first have some string content in an input box on a web form, and use push button 2 to save this text to a file
you open the file say in notepad, content should be fine
then you click on push button 3, and want to open the file and read back the contents ...
this content is then passed as a QString to another javascript function, where you should be able to access the content...
is my understanding correct?
-
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