使用 QtWebkit 的 javascript 桥,我创建了一个类来将我的 web 框架中的数据与我的 Qt 代码的其余部分连接起来。它识别对象,但不识别它的任何方法。
mainwindow.cpp 代码:
#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 代码:
#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不能调用app类的函数,各位帮帮我谢谢