first
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
#include "wke.h"
|
||||
#include "mapwidget.h"
|
||||
|
||||
#include <QApplication>
|
||||
#include <QFile>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
wkeSetWkeDllPath(L"node.dll");
|
||||
wkeInitialize();
|
||||
/* webView在头文件里面声明了是一个wkeWebView类型
|
||||
wkeCreateWebWindow是创建一个带真实窗口的wkeWebView
|
||||
第一个参数wkeWindowType type:
|
||||
WKE_WINDOW_TYPE_POPUP: 普通窗口
|
||||
WKE_WINDOW_TYPE_TRANSPARENT:透明窗口。mb内部通过layer window实现
|
||||
WKE_WINDOW_TYPE_CONTROL: 嵌入在父窗口里的子窗口。此时parent需要被设置
|
||||
第二个参数是依附parent窗口的句柄, 在QT里面可以调用窗口的winID()方法获取
|
||||
*/
|
||||
MapWidget w;
|
||||
|
||||
w.show();
|
||||
// WebWidget *w = new WebWidget("www.baidu.com");
|
||||
|
||||
|
||||
int ret = a.exec();
|
||||
wkeFinalize();
|
||||
return ret;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
#include "mapwidget.h"
|
||||
#include "ui_mapwidget.h"
|
||||
|
||||
#include <QWebSocket>
|
||||
|
||||
MapWidget::MapWidget(QWidget *parent) :
|
||||
QWidget(parent),
|
||||
ui(new Ui::MapWidget)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
|
||||
this->showMaximized();
|
||||
m_pWebWidget = new WebWidget("D:/4_Code/Leaflet/demo.html", this);
|
||||
ui->verticalLayout->addWidget(m_pWebWidget);
|
||||
}
|
||||
|
||||
MapWidget::~MapWidget()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void MapWidget::on_btn_callWmts_clicked()
|
||||
{
|
||||
m_pWebWidget->callJsFunction("printInformation", "tian");
|
||||
return;
|
||||
m_pWebWidget->startWmts();
|
||||
}
|
||||
|
||||
|
||||
void MapWidget::on_btn_server_clicked()
|
||||
{
|
||||
if(!m_pWebSocketServer)
|
||||
{
|
||||
//构造:QWebSocketServer(const QString& serverName,QWebSocketServer::SslMode secureMode,QObject *parent=nullptr)
|
||||
//使用给定的serverName构造一个新的QWebSocketServer。
|
||||
//该服务器名称将在HTTP握手阶段被用来识别服务器。它可以为空,此时不会将服务器名称发送给客户端。
|
||||
//SslMode指示服务器是通过wss(SecureMode)还是ws(NonSecureMode)运行
|
||||
//QWebSocketServer::SecureMode服务器以安全模式运行(通过wss)
|
||||
m_pWebSocketServer = new QWebSocketServer("Server", QWebSocketServer::NonSecureMode, this);
|
||||
}
|
||||
// QHostAddress address = QHostAddress("127.0.0.1");
|
||||
int port = 12345;
|
||||
QString s;
|
||||
if(!m_pWebSocketServer->listen(QHostAddress::Any, port))
|
||||
{
|
||||
s = "websocket listen error";
|
||||
}
|
||||
else
|
||||
{
|
||||
s = QString("websocket listen on port %1").arg(port);
|
||||
connect(m_pWebSocketServer, &QWebSocketServer::newConnection, this, &MapWidget::onNewConnection);
|
||||
connect(m_pWebSocketServer, &QWebSocketServer::closed, this, &MapWidget::onWebSocketClosed);
|
||||
}
|
||||
qDebug() << s;
|
||||
m_pWebWidget->callJsFunction("printInformation", s);
|
||||
}
|
||||
|
||||
void MapWidget::onNewConnection()
|
||||
{
|
||||
QWebSocket *socket = m_pWebSocketServer->nextPendingConnection();
|
||||
if(!socket)
|
||||
return;
|
||||
clientList.push_back(socket);
|
||||
qDebug() << QString("[New Connect] Address:%1 Port:%2")
|
||||
.arg(socket->peerAddress().toString())
|
||||
.arg(socket->peerPort());
|
||||
|
||||
//收到消息
|
||||
connect(socket, &QWebSocket::textMessageReceived, this, [&](const QString & msg)
|
||||
{
|
||||
qDebug() << msg;
|
||||
m_pWebWidget->callJsFunction(msg);
|
||||
});
|
||||
//发送消息
|
||||
connect(this, &MapWidget::doSendMessage, socket, &QWebSocket::sendTextMessage);
|
||||
//断开连接,释放
|
||||
connect(socket, &QWebSocket::disconnected, this, [&, socket]()
|
||||
{
|
||||
clientList.removeAll(socket);
|
||||
socket->deleteLater();
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
void MapWidget::onWebSocketClosed()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
void MapWidget::on_pushButton_clicked()
|
||||
{
|
||||
static int cnt = 0;
|
||||
emit doSendMessage(QString("send message %1").arg(cnt++));
|
||||
}
|
||||
|
||||
|
||||
void MapWidget::on_btn_clear_clicked()
|
||||
{
|
||||
static int cnt1 = 3;
|
||||
m_pWebWidget->callJsFunction("clearOneLine", QString::number(cnt1--));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
#ifndef MAPWIDGET_H
|
||||
#define MAPWIDGET_H
|
||||
|
||||
#include <QWebSocketServer>
|
||||
#include <QWidget>
|
||||
#include "webwidget.h"
|
||||
|
||||
namespace Ui
|
||||
{
|
||||
class MapWidget;
|
||||
}
|
||||
|
||||
class MapWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit MapWidget(QWidget *parent = nullptr);
|
||||
~MapWidget();
|
||||
|
||||
signals:
|
||||
void doSendMessage(const QString & s);
|
||||
|
||||
private slots:
|
||||
void on_btn_callWmts_clicked();
|
||||
|
||||
void on_btn_server_clicked();
|
||||
|
||||
void onNewConnection();
|
||||
|
||||
void onWebSocketClosed();
|
||||
|
||||
void on_pushButton_clicked();
|
||||
|
||||
void on_btn_clear_clicked();
|
||||
|
||||
private:
|
||||
Ui::MapWidget *ui;
|
||||
|
||||
WebWidget * m_pWebWidget = nullptr;
|
||||
|
||||
QWebSocketServer *m_pWebSocketServer = nullptr;
|
||||
QList<QWebSocket*> clientList;
|
||||
};
|
||||
|
||||
#endif // MAPWIDGET_H
|
||||
@@ -0,0 +1,79 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>MapWidget</class>
|
||||
<widget class="QWidget" name="MapWidget">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>1386</width>
|
||||
<height>1030</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Form</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_3">
|
||||
<item row="0" column="0">
|
||||
<widget class="QWidget" name="widget" native="true">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout">
|
||||
<item row="0" column="0">
|
||||
<layout class="QVBoxLayout" name="verticalLayout"/>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="1">
|
||||
<widget class="QGroupBox" name="groupBox">
|
||||
<property name="sizePolicy">
|
||||
<sizepolicy hsizetype="Minimum" vsizetype="Preferred">
|
||||
<horstretch>0</horstretch>
|
||||
<verstretch>0</verstretch>
|
||||
</sizepolicy>
|
||||
</property>
|
||||
<property name="title">
|
||||
<string>GroupBox</string>
|
||||
</property>
|
||||
<layout class="QGridLayout" name="gridLayout_2">
|
||||
<item row="2" column="0">
|
||||
<widget class="QPushButton" name="pushButton">
|
||||
<property name="text">
|
||||
<string>PushButton</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="1" column="0">
|
||||
<widget class="QPushButton" name="btn_server">
|
||||
<property name="text">
|
||||
<string>serverListen</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="0" column="0">
|
||||
<widget class="QPushButton" name="btn_callWmts">
|
||||
<property name="text">
|
||||
<string>CallWMTS</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item row="3" column="0">
|
||||
<widget class="QPushButton" name="btn_clear">
|
||||
<property name="text">
|
||||
<string>清除所有曲线</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
@@ -0,0 +1,77 @@
|
||||
#include "webwidget.h"
|
||||
#include "qapplication.h"
|
||||
#include <QHBoxLayout>
|
||||
#include <QDebug>
|
||||
WebWidget::WebWidget(const QString& url, QWidget *_parent) : QWidget(_parent)
|
||||
{
|
||||
webView = wkeCreateWebWindow(WKE_WINDOW_TYPE_CONTROL, (HWND)_parent->winId(), 0, 0, this->width(), this->height());
|
||||
wkeShowWindow(webView, TRUE);
|
||||
QString _sUrl = url;
|
||||
if(_sUrl.isEmpty())
|
||||
_sUrl = "www.baidu.com";
|
||||
wkeLoadURL(webView, _sUrl.toUtf8());
|
||||
// connect()
|
||||
}
|
||||
|
||||
WebWidget::~WebWidget()
|
||||
{
|
||||
qDebug() << "in del";
|
||||
}
|
||||
void WebWidget::loadUrl(const QString& url)
|
||||
{
|
||||
QString _sUrl = url;
|
||||
if(_sUrl.isEmpty())
|
||||
_sUrl = "www.baidu.com";
|
||||
wkeLoadURL(webView, _sUrl.toLocal8Bit().data());
|
||||
}
|
||||
void WebWidget::resizeEvent(QResizeEvent *event)
|
||||
{
|
||||
Q_UNUSED(event);
|
||||
wkeResize(webView, this->width(), this->height());
|
||||
QPoint pos = this->mapToGlobal(QPoint(0, 0));
|
||||
wkeMoveWindow(webView, 0, 0, this->width() + pos.x(), this->height() + pos.y() / 2);
|
||||
}
|
||||
|
||||
void WebWidget::statusFeedbackSlot()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void WebWidget::callJsFunction(const QString &sFunctionName, const QString &sPara)
|
||||
{
|
||||
QString s = sFunctionName + "(\"" + sPara + "\");";
|
||||
wkeRunJS(webView, s.toUtf8());
|
||||
}
|
||||
|
||||
void WebWidget::onMove()
|
||||
{
|
||||
QPoint pos = this->mapToGlobal(QPoint(0, 0));
|
||||
wkeMoveWindow(webView, pos.x(), pos.y(), this->width(), this->height());
|
||||
}
|
||||
|
||||
void WebWidget::slotProcessQuit(int, QProcess::ExitStatus )
|
||||
{
|
||||
if(m_pWmtsProcess)
|
||||
{
|
||||
disconnect(m_pWmtsProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotProcessQuit(int, QProcess::ExitStatus)));
|
||||
// 负责进程间通信
|
||||
disconnect(m_pWmtsProcess, &QProcess::readyReadStandardError, this, &WebWidget::statusFeedbackSlot);
|
||||
disconnect(m_pWmtsProcess, &QProcess::readyReadStandardOutput, this, &WebWidget::statusFeedbackSlot);
|
||||
m_pWmtsProcess->deleteLater();
|
||||
m_pWmtsProcess = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void WebWidget::startWmts()
|
||||
{
|
||||
if(m_pWmtsProcess != nullptr)
|
||||
return;
|
||||
m_pWmtsProcess = new QProcess;
|
||||
QString path = QCoreApplication::applicationDirPath();
|
||||
m_pWmtsProcess->setProgram(path + "/wmts.exe");
|
||||
m_pWmtsProcess->start(QProcess::ReadWrite);
|
||||
connect(m_pWmtsProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(slotProcessQuit(int, QProcess::ExitStatus)));
|
||||
// 负责进程间通信
|
||||
connect(m_pWmtsProcess, &QProcess::readyReadStandardError, this, &WebWidget::statusFeedbackSlot);
|
||||
connect(m_pWmtsProcess, &QProcess::readyReadStandardOutput, this, &WebWidget::statusFeedbackSlot);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#ifndef WEBWIDGET_H
|
||||
#define WEBWIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
#include <QResizeEvent>
|
||||
#include <QFileInfo>
|
||||
#include "wke.h"
|
||||
#include <QProcess>
|
||||
|
||||
class WebWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit WebWidget(const QString& url = "", QWidget *parent = 0);
|
||||
~WebWidget();
|
||||
|
||||
/**
|
||||
* @brief loadUrl 打开网页
|
||||
* @param url 网页url
|
||||
*/
|
||||
void loadUrl(const QString &url);
|
||||
|
||||
void onMove();
|
||||
|
||||
void startWmts();
|
||||
|
||||
/**
|
||||
* @brief callJsFunction 调用js中的函数
|
||||
* @param sFunctionName 函数名称
|
||||
* @param sPara 函数参数,如数值a和b,传入形式为QString("%1,%2").arg(a).arg(b)
|
||||
*/
|
||||
void callJsFunction(const QString &sFunctionName, const QString &sPara = "");
|
||||
signals:
|
||||
|
||||
protected:
|
||||
void resizeEvent(QResizeEvent *event);
|
||||
|
||||
public slots:
|
||||
void statusFeedbackSlot();
|
||||
|
||||
void slotProcessQuit(int exitCode, QProcess::ExitStatus stuts);
|
||||
|
||||
private:
|
||||
wkeWebView webView;
|
||||
|
||||
QProcess *m_pWmtsProcess = nullptr;
|
||||
};
|
||||
|
||||
#endif // WEBWIDGET_H
|
||||
Reference in New Issue
Block a user