转自:
http://blog.163.com/qimo601@126/blog/static/15822093201682185819623/
Qt 4.8.4
感谢原作者,我只转载。
看看作者如何实现:
1、Qt源码调用html中js的函数disp_messagebox();
2、js源码中如何调用qt中的函数
MainWindow.jsInvokeQt();
MainWindow.setInfor("Qt change string");
alert(MainWindow.getInfor());
页面载入完成的信号:
connect(ui.webView->page()->mainFrame(), SIGNAL(loadFinished(bool)),
this, SLOT(callFunction()));
this, SLOT(callFunction()));
由于篇幅限制,我就不发头文件了,详细源码可以下载附件QtInvokeJS.zip
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
/* 载入html网页 */
m_pWebView = new QWebView();
m_pWebView->load(QUrl("file:///D:/jsTest.html"));
m_pWebView->show();
/* 开启JavaScript支持 */
QWebSettings *pWebSettings = m_pWebView->page()->settings();
pWebSettings->setAttribute(QWebSettings::JavascriptEnabled,true);
/* 窗体布局, 添加按钮 */
QPushButton *pEventBtn = new QPushButton(tr("QT Invoke JavaScript web page Object"));
QPushButton *pcloseBtn = new QPushButton(tr("close"));
/* 布局:网页与按钮为上下布局,两按钮为水平居中布局 */
QHBoxLayout *pBtnLayout = new QHBoxLayout;
pBtnLayout->addWidget(pEventBtn);
pBtnLayout->addWidget(pcloseBtn);
pBtnLayout->setAlignment(Qt::AlignCenter);
QVBoxLayout *pMainLayout = new QVBoxLayout;
pMainLayout->addWidget(m_pWebView);
pMainLayout->addLayout(pBtnLayout);
QWidget *widget = new QWidget;
widget->setLayout(pMainLayout);
setCentralWidget(widget);
/* 建立信号与槽, 每次载入html时发送段信号 */
connect(m_pWebView->page()->mainFrame(),SIGNAL(javaScriptWindowObjectCleared()),
this,SLOT(addObjectToJs()));
connect(pEventBtn,SIGNAL(clicked()),this,SLOT(testJs()));
connect(pcloseBtn,SIGNAL(clicked()),this,SLOT(close()));
testStr.clear();
}
MainWindow::~MainWindow()
{
if(m_pWebView != NULL)
{
delete m_pWebView;
m_pWebView = NULL;
}
/* 必须的,否则,在debug编译模式下就会出现leak错误 */
QWebSettings::globalSettings()->clearMemoryCaches();
delete ui;
}
/* 这个public slot槽函数是为了响应JavaScript的的invoke的 */
void MainWindow::jsInvokeQt()
{
qDebug()<<"MainWindow::jsInvokeQt(),\t JavaScript invoke Qt program!";
}
/* 将MainWindows这个类的名称&&对象指针发送给JavaScript */
void MainWindow::addObjectToJs()
{
m_pWebView->page()->mainFrame()->addToJavaScriptWindowObject("MainWindow",this);
}
/* Qt来 invoke JavaScript里面的disp_messagebox()函数 */
void MainWindow::testJs()
{
qDebug()<<"MainWindow::testJs(),\t Qt invoke JavaScript's function";
m_pWebView->page()->mainFrame()->evaluateJavaScript("disp_messagebox()");
}
/* JavaScript invoke Qt里面的public slot 槽函数 */
void MainWindow::setInfor(const QString str)
{
qDebug()<<"MainWindow::setInfor(const QString str),\t Response of js Invoke!"<<"var is:"<<str;
if(str.isEmpty())
{
qDebug()<<"str is empty!";
return;
}
else
{
testStr = str;
}
}
/* JavaScript invoke Qt里面的public slot 槽函数 */
QString MainWindow::getInfor()
{
qDebug()<<"MainWindow::GetInfor(),\t "<<testStr;
return testStr;
}
-----------------------------------------------------------------------------
<html>
<head>
<script type=
"text/javascript"
>
function disp_messagebox()
{
alert(
"This is javaScript MessageBox come from alert!"
)
}
function disp_qtmessage()
{
alert(
"disp_messagebox function, who is in JavaScript!"
)
MainWindow.jsInvokeQt();
MainWindow.setInfor(
"Qt change string"
);
alert(MainWindow.getInfor());
}
</script>
</head>
<body>
<input type=
"button"
onclick=
"disp_qtmessage()"
value=
"JavaScript Invoke Qt Object"
/>
</body>
</html>