#include "widget.h"
#include "ui_widget.h"
#include "mybridgeofjs.h"
#include <QPushButton>
#include <QLineEdit>
#include <QDoubleSpinBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QDir>
#include <QDebug>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
resize(1300,800);
QGridLayout* mainLayout = new QGridLayout(this);
QHBoxLayout* pHBoxLayout = new QHBoxLayout;
pHBoxLayout->addWidget(ui->label_jingdu);
pHBoxLayout->addWidget(ui->doubleSpinBox_jingdu);
pHBoxLayout->addSpacing(10);
pHBoxLayout->addWidget(ui->label_weidu);
pHBoxLayout->addWidget(ui->doubleSpinBox_weidu);
pHBoxLayout->addSpacing(20);
pHBoxLayout->addWidget(ui->pushButton_mark);
pHBoxLayout->addStretch(1);
pHBoxLayout->addWidget(ui->label_pos);
pHBoxLayout->addWidget(ui->lineEdit_pos);
ui->label_pos->setVisible(false);
ui->lineEdit_pos->setVisible(false);
m_pWebView = new QWebEngineView(this);
// 关闭自动代理
QNetworkProxyFactory::setUseSystemConfiguration(false);
//创建通道对象用于与JS交互
m_pWebchannel = new QWebChannel(this);
MybridgeofJS *mybride = new MybridgeofJS();
m_pWebchannel->registerObject("bridge_name",(QObject*)mybride);
// 这里注册对象名"bridge_name"需要与JS函数用到的名称一致
//见baidumap.html文件第94行 window.bridge_js = channel.objects.bridge_name;
// C++对象"bridge_name", JS对象bridge_js。101行,JS调用QT中C++函数只需
// bridge_js.getCoordinate(e.point.lng, e.point.lat);网页向QT传递经纬度坐标
//这就实现了JS调用C++函数,实现网页向QT传递数据
m_pWebView->page()->setWebChannel(m_pWebchannel);
QDir temDir("../MyWebEngineJS/baidumap.html");
QString absDir = temDir.absolutePath();
QString filePath = "file:///" + absDir;
qDebug()<<filePath;
m_pWebView->page()->load(QUrl(filePath));
// 加载本地网页
//m_pWebView->load(QUrl("https://www.baidu.com"));
mainLayout->addLayout(pHBoxLayout,0,0,1,1);
mainLayout->addWidget(m_pWebView,1,0,1,1);
}
void Widget::on_pushButton_mark_clicked()
{
//把要调用的JS命令当做QString传递给网页
QString cmd = QString("add_Point(%0,%1)").arg(ui->doubleSpinBox_jingdu->value()).arg(ui->doubleSpinBox_weidu->value());
qDebug() << cmd;
//实现QT通过C++调用JS函数
m_pWebView->page()->runJavaScript(cmd);
}
Widget::~Widget()
{
delete ui;
}
评论1