IE中JS与C++交互 qt

#include <QAxWidget>
#include <QAxWidget>
#include <QWebFrame>
#include <QDebug>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QMessageBox>
#include <QDir>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
	
	showMaximized();
    showMaximized();
    webView = new QWebView(this->centralWidget());
    webView->load(QUrl("./test.html"));
    webView->setMaximumWidth(200);
    QPushButton *btClose = new QPushButton(this);
    btClose->setText(QString::fromLocal8Bit("调用html页面接口"));
    btClose->setMaximumWidth(150);
    QVBoxLayout *vLayout = new QVBoxLayout();
    vLayout->addWidget(webView);
    vLayout->addWidget(btClose); 
    connect(webView, SIGNAL(loadFinished(bool)), this, SLOT(slotLoadFinished(bool)));
    connect(btClose, SIGNAL(clicked()), this, SLOT(slotSetToWeb()));

}

void MainWindow::slotLoadFinished( bool )
{
     //  类对象 添加到 html页面。
	 webView->page()->mainFrame()->addToJavaScriptWindowObject("MainWindow", this);
}
void MainWindow::submit_webcb( const QString &userInfo )
{	
	 QMessageBox::information(this,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("本地接口调用成功"),QMessageBox::Ok);
}

void MainWindow::slotSetToWeb()
{
	QString userInfo ="111111111";
	webView->page()->mainFrame()->evaluateJavaScript(QString("setUserInfo('%1')").arg(userInfo));
}
<!DOCTYPE html>
<html>
<head>
<title> tree</title>
    <script type="text/javascript">
        function setUserInfo(userInfo) {  // qt版调用本地接口
            alert("我是 html 接口");   
        }
        function submitUserInfo() {  // qt版调用本地接口
		    //alert("调用本地接口");
            var result = "1111111";
            MainWindow.submit_webcb(result);  
        } 		
		function Add(value1, value2) { 
		alert("我是 html add 接口");
        return value1 + value2; 
       }   
	   function MyMethod() {
		alert(11111111111111111);
		window.external.MyMethod();
      }	
    </script>
</head>
<body>
<form action="" onsubmit="submitUserInfo()">
<input type="submit" value="调用本地接口"/>
</form>
<form action="" onsubmit="MyMethod()">
<input type="submit" value="MyMethod"/>
</form>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值