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 :

Qt Code:
  1. void Test::on_pushButton3_clicked() /// This function is activated when I click open button
  2. {
  3. QString output;
  4. QString fileName = QFileDialog::getOpenFileName(this);
  5. qDebug() << fileName;
  6. if (!fileName.isEmpty()) {
  7. // read from file
  8. QFile file(fileName);
  9.  
  10.  
  11. if (!file.open(QIODevice::ReadOnly)) {
  12. QMessageBox::information(this, tr("Unable to open file"),
  13. file.errorString());
  14. return;
  15. }
  16.  
  17.  
  18. QDataStream in(&file);
  19. in.setVersion(QDataStream::Qt_4_5);
  20. in >> output;
  21. qDebug() << output; /// output is the string I need to send to JavaScript open() function. I checked it using qDebug and its
  22. //// working fine.
  23. webView->page()->mainFrame()->evaluateJavaScript(QString("open(\"%1\")").arg(output));
  24. }
  25. }
To copy to clipboard, switch view to plain text mode 

Here is the code for saving the file :

Qt Code:
  1. void Test::on_pushButton2_clicked()
  2. {
  3. {
  4. QVariant content = webView->page()->mainFrame()->evaluateJavaScript("dosave()");
  5. QString data = content.toString();
  6. qDebug() << data;
  7. QString fileName = QFileDialog::getSaveFileName(this);
  8.  
  9. if (!fileName.isEmpty()) {
  10. // save to file
  11. QFile file(fileName);
  12.  
  13. if (!file.open(QIODevice::WriteOnly)) {
  14. QMessageBox::information(this, tr("Unable to open file"),
  15. file.errorString());
  16. return;
  17. }
  18.  
  19. QDataStream out(&file);
  20. out.setVersion(QDataStream::Qt_4_5);
  21. out << data;
  22. }
  23. }
  24. }
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.