Regarding Qt bridge javascript
-
I am new to QT. Trying to use QT bridge to call c++ API from javascript. Have added a QWebView using QT GUI. When I launch the app and click on the html input Qt callback is not called. Constructer msg for MyJavaScriptOperations is printed when app is launched. So I believe the bridge object has been successfully created. I do see that the alert message in html code (see code) is called. Also, when I move the alert message after "myoperations.display(6)" call, alert msg is not shown. I must mention here, similar code works when the webview object is created in main.cpp. It doesn't work when the webview is created in qt creator GUI.
Am I doing something wrong here? Appreciate your help.
Important code snippets are as follows.
main.cpp
*QApplication a(argc, argv);
MainWindow win;win.setupWeb();
win.loadPage("http://localhost/test.html");
win.show();
return(a.exec);*MainWindow.cpp
*class MyJavaScriptOperations : public QObject {
Q_OBJECT
public:
MyJavaScriptOperations(){qDebug() << "In constructor"; display(5);
}
public slots:
Q_INVOKABLE void display(int index){
qDebug() << "In display method "<< index;
}
};
void MainWindow::setupWeb()
{
ui->webView->settings()->setAttribute(QWebSettings::PluginsEnabled, true);
}void MainWindow::loadPage(QString page)
{
ui->webView->page()->mainFrame()->addToJavaScriptWindowObject("myoperations", new MyJavaScriptOperations);
ui->webView->load(QUrl(page));
}*test.html
*<html>
<body>
<script type="text/javascript">function displayMessage(){
alert("Hello display");
myoperations.display(6);
}
</script><p>Test Example to call Qt function from Javascript </p>
<input type="button" value="Click me!" onclick="displayMessage()">
</body>
</html> *