Files
energy_storage/src/widgets/WidgetWeb.cpp
2025-05-19 09:54:33 +08:00

41 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#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)
{
}