September 27, 2011

atena_2019 atena_2019
Lab Rat
9 posts

connect qwebkit and javascript problem

 

hi to all
i am using qwebkit
i have a cpp object and export it to javascript but it does not connect to it , because it does not work
here is my code : my cpp code :

  1. #include "mainwindow.h"
  2. #include "ui_mainwindow.h"
  3. #include <QWebFrame>
  4. #include <QMessageBox>
  5.  
  6. MainWindow::MainWindow(QWidget *parent) :
  7.     QMainWindow(parent),
  8.     ui(new Ui::MainWindow)
  9. {
  10.     ui->setupUi(this);
  11.     this->setObjectName("MyWin");
  12.     connectCPPObjectToJS(this->objectName() , this);
  13.     loadUrl(QCoreApplication::applicationDirPath() + "/sura.htm");
  14.  
  15.  }
  16.  
  17.  
  18. void MainWindow::test()
  19. {
  20.     QMessageBox::information(this , "a" , "hello");
  21. }
  22.  
  23. void MainWindow::loadUrl(const QString & localpath)
  24. {
  25.     ui->webView->load(QUrl::fromLocalFile(localpath));
  26. }
  27.  
  28. void MainWindow::on_comboBox_currentIndexChanged(int index)
  29. {
  30.     QString s ;
  31.     QMessageBox::information(this , "index change" , s.setNum(index));
  32.     emit sendChangedIndexToJS(index);
  33.  
  34. }
  35.  
  36. void MainWindow::connectCPPObjectToJS(const QString objectname , QObject* objecttype)
  37. {
  38.     ui->webView->page()->mainFrame()->addToJavaScriptWindowObject( objectName() , objecttype);
  39.  
  40. }
  41.  
  42. void MainWindow::on_comboBox_textChanged(QString a)
  43. {
  44.     QMessageBox::information(this , "text change" , a);
  45.  
  46. }

and here is my html code :

  1. <html>
  2.  
  3. <head>
  4. <meta http-equiv="Content-Language" content="en-us">
  5. <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
  6. <title>&#1605;&#1579;&#1575;&#1604; &#1670;&#1607;&#1575;&#1585; &#1602;&#1604;</title>
  7. [removed]
  8. function updatesura(value){
  9. alert("the value was changed to " + value);
  10. }
  11. MyWin.test();
  12. MyWin.sendChangedIndexToJS.connect(updatesura());
  13.  
  14.  [removed]
  15.  
  16. </head>
  17.  
  18. <body>
  19. <p align="center"><font face="Tahoma">&#1587;&#1608;&#1585;&#1607;</font>
  20. </p>
  21. </body>
  22.  
  23. </html>

what is wrong ?
thanks a lot

3 replies

September 27, 2011

atena_2019 atena_2019
Lab Rat
9 posts

i put the statement javascript “MyWin.test();” in a try catch and the error is that :
ReferenceError : can’t find variable MyWin

so now i am sure that the connection is wrong . but i do not know why:(

September 27, 2011

peppe peppe
Ant Farmer
1025 posts

Quoting the docs:

If you want to ensure that your QObjects remain accessible after loading a new URL, you should add them in a slot connected to the javaScriptWindowObjectCleared() signal.

 Signature 

Software Engineer
KDAB (UK) Ltd., a KDAB Group company

September 27, 2011

atena_2019 atena_2019
Lab Rat
9 posts

thanks a lot . you are right

 
  ‹‹ WebSocket draft?      example of connect qwebkit and javascript ››

You must log in to post a reply. Not a member yet? Register here!