Qt调用JS
Posted 邶风
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Qt调用JS相关的知识,希望对你有一定的参考价值。
转自:http://www.cnblogs.com/verstin/p/4908673.html
<html>
<script language="javascript">
function qtcalljs()
{
alert("sdfsd");
}
function jscallqt()
{
mainWindow.dosome();
}
</script>
<body>
<button onclick="jscallqt()" style=="height:20px;width:100px"/>
</body>
</html>
--------------
#ifndef QTTT_H
#define QTTT_H
#include <QtGui/QMainWindow>
#include <QWebKitPlatformPlugin>
#include <QWebFrame>
#include <QMessageBox>
#include <QObject>
#include "ui_qttt.h"
class qttt : public QMainWindow
{
Q_OBJECT
public:
qttt(QWidget *parent = 0, Qt::WFlags flags = 0);
~qttt();
private:
Ui::qtttClass ui;
private slots:
void pop();
void addMyObjectToJavascript();
public:
Q_INVOKABLE void dosome();
};
#endif // QTTT_H
--------------------------------------------
#include "qttt.h"
qttt::qttt(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
ui.webView->load(QUrl("test.html"));
connect(ui.pushButton,SIGNAL(clicked()),this,SLOT(pop()));
connect(ui.webView->page()->mainFrame(), SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(addMyObjectToJavascript()));
}
qttt::~qttt()
{
}
void qttt::pop()
{
ui.webView->page()->mainFrame()->evaluateJavaScript("qtcalljs();");
}
void qttt::addMyObjectToJavascript()
{
ui.webView->page()->mainFrame()->addToJavaScriptWindowObject("mainWindow", this);
}
void qttt::dosome()
{
QMessageBox::aboutQt(this,"sdfsa");
}
以上是关于Qt调用JS的主要内容,如果未能解决你的问题,请参考以下文章