PDA

View Full Version : Problems in Implementing Save Feature in Javascript app using Qt



qt.learner
25th September 2010, 15:28
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.