Widget::Widget(QWidget *parent) :
QWidget(parent)
{
webWidget = new QAxWidget;
webWidget->setControl(QString::fromUtf8("{8856F961-340A-11D0-A96B-00C04FD705A2}"));
loadNavigate();
webWidget->setObjectName(QString::fromUtf8("webWidget"));
webWidget->setFocusPolicy(Qt::StrongFocus);
webWidget->setProperty("DisplayAlerts",false); //不显示任何警告信息。
webWidget->setProperty("DisplayScrollBars",false); // 显示滚动条
connect(webWidget,SIGNAL(NavigateComplete2(IDispatch*,QVariant& )),
this,SLOT(sltNavigateComplete2(IDispatch*,QVariant&)));
QPushButton *reloadButton = new QPushButton("Reload");
connect(reloadButton,SIGNAL(clicked()),this,SLOT(sltReloadButtonClick()));
QPushButton *testButton = new QPushButton("Draw");
connect(testButton,SIGNAL(clicked()),this,SLOT(sltTestButtonClick()));
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addStretch();
buttonLayout->addWidget(reloadButton);
buttonLayout->addStretch();
buttonLayout->addWidget(testButton);
buttonLayout->addStretch();
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(webWidget);
mainLayout->addLayout(buttonLayout);
this->setLayout(mainLayout);
this->resize(1000,500);
}
下面两个地址为本地地图,测试可用
void Widget::loadNavigate()
{
webWidget->dynamicCall("Navigate(const QString&)","file:///E:/MyProjects/MWebBrowser/MWebBrowser/2.html");
//webWidget->dynamicCall("Navigate(const QString&)","http://192.168.2.163/gisflash/maintest.jsp");
//webWidget->dynamicCall("Navigate(const QString&)","http://localhost:9080/centranGIS/mapJsp.html");
}
void Widget::sltTestButtonClick()
{
QAxObject *document = webWidget->querySubObject("Document");
QAxObject *parentWindow = document->querySubObject("parentWindow");
parentWindow->dynamicCall("execScript(QString,QString)","Test()","JavaScript");
/*
// 以下是地图加点的操作
QString js;
js = "DrawMarker('{\"id\":1,\"label\":\"4444\",\"lat\":39.945824060,\"lon\":116.28546870,\"type\":1}')";
parentWindow->dynamicCall("execScript(QString,QString)",js,"JavaScript");
*/
/*
// 获取webWidget的文档说明
QString doc = webWidget->generateDocumentation();
QFile outFile("E:/MyProjects/MWebBrowser/webWidget.html");
outFile.open(QIODevice::WriteOnly|QIODevice::Append);
QTextStream ts(&outFile);
ts<<doc<<endl;
*/
}
void Widget::sltNavigateComplete2(IDispatch *pDisp, QVariant &URL)
{
qDebug() << "hello";
}
void Widget::sltReloadButtonClick()
{
loadNavigate();
}
2.html文件
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script type="text/javascript">
function Test(){
alert("你调用了全局函数Test");
}
</script>
</head>
<body>
<input type="button" value="click me" οnclick="Test();" />
</body>
</html>