Using QtWebkit's javascript bridge, I have created a class to interface the data in my web frame with the rest of my Qt code. it recognises the object, but none of its methods.
mainwindow.cpp code:
#include "app.h"
MainWindow::MainWindow(QWidget *parent) : QWebView(parent)
{
happ = new app(this);
m_network = new QNetworkAccessManager(this);
page()->setNetworkAccessManager(m_network);
QFile file("E://qt//test.happ//index.html");
file.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream in(&file);
QString htmlContent = in.readAll();
addJSObject();
QObject::connect(page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()),this, SLOT(addJSObject()));
setHtml(htmlContent);
}
void MainWindow::addJSObject()
{
page()->mainFrame()->addToJavaScriptWindowObject(QString("happ"), happ);
};
app.h code:
#include <QObject>
class app:public QObject
{
public:
app(QObject *parent);
public slots:
void os_foo();
signals:
void win_bar();
};
javascript:
function a(){
if(window.happ){
alert("obj: " + typeof happ); //shows "obj: object"
alert("os_foo: " + typeof happ.os_foo); //shows "os_foo: undefined"
}
}
javascript can not call the function of the app class, you help me Thank you