#include "WidgetWeb.h" WebHandler::WebHandler(QObject* parent) : QObject {parent} { webChannel = new QWebChannel(); // 注册C++对象到QWebChannel,这样远端的QWebChannel也会生成一个对应的JS对象 // 在JS中引入 qwebchannel.js, 使用 channel.objects.webNative 获取注册的“webNative” webChannel->registerObject("webNative", this); } void WebHandler::setNativeText(const QString& text) { m_nativeText = text; qDebug() << QString("setNativeText:") << text; emit signalNativeTextChanged(text); } WidgetWeb::WidgetWeb(QWidget* parent) : QWebEngineView(parent) { webHandler_ = new WebHandler(); this->page()->setWebChannel(webHandler_->webChannel); this->setContextMenuPolicy(Qt::NoContextMenu); } void WidgetWeb::loadHtml(std::string url) { // "file:///assets/html/echarts//maptest.html" this->load(QUrl(url.c_str())); connect(this, &QWebEngineView::loadFinished, this, &WidgetWeb::slotLoadFinished); } void WidgetWeb::invikeJS(QString jscode) { //QString jscode = QString("showalert('%1')").arg("Hello QtWebEngine!"); this->page()->runJavaScript(jscode, [](const QVariant& v) { qDebug() << v.toString(); }); } void WidgetWeb::slotLoadFinished(bool ret) { }