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
{
qDebug() << fileName;
if (!fileName.isEmpty()) {
// read from file
QMessageBox::information(this, tr
("Unable to open file"),
file.errorString());
return;
}
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
));
}
}
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));
}
}
To copy to clipboard, switch view to plain text mode
Here is the code for saving the file :
void Test::on_pushButton2_clicked()
{
{
QVariant content
= webView
->page
()->mainFrame
()->evaluateJavaScript
("dosave()");
qDebug() << data;
if (!fileName.isEmpty()) {
// save to file
QMessageBox::information(this, tr
("Unable to open file"),
file.errorString());
return;
}
out << data;
}
}
}
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;
}
}
}
To copy to clipboard, switch view to plain text mode
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.
Bookmarks