QWebXX

news/2025/2/19 12:24:58/

加载JS

html内部直接加入js

出现问题: qt is not defined =》 分离html与QT,JS通过QT代码注入

注入(html内部不包含此js文件引用)

runJavaScript()

connect(m_WebView, &QWebEngineView::loadFinished, [=]() {m_WebView->page()->runJavaScript(bywebjs);});

QWebEngineProfile: scripts()->insert()

QWebEngineProfile *profile = QWebEngineProfile::defaultProfile();QWebEngineScript script;
//    script.setName("qwebchannel.js");
//    script.setWorldId(QWebEngineScript::MainWorld);
//    script.setInjectionPoint(QWebEngineScript::DocumentCreation);
//    script.setRunsOnSubFrames(false);
script.setSourceCode(bywebjs);
profile->scripts()->insert(script);
m_WebView->load(QUrl(strHtml));

调试

https://doc.qt.io/QT-5/qtwebengine-debugging.html

qputenv("QTWEBENGINE_REMOTE_DEBUGGING", "5566");

若不生效,可在命令行中添加 --remote-debugging-port=<port_number>
在这里插入图片描述

外部资源引入,一定带上QUrl::fromLocalFile()

m_WebView->load(QUrl::fromLocalFile(strHtml));

QWebChannel: 与js交互

  1. 创建通信类
m_Report = new DReportChannelCmd(this);
  1. 创建QWebChannel通道
m_pWebChannel = new QWebChannel(this);
  1. 注册C++交互类对象
m_pWebChannel->registerObject("context", m_Report);  // js获取qt对象时所使用的索引名channel.objects.context
  1. 设置为当前页面的通道
m_pWebView->page()->setWebChannel(m_pWebChannel);
  1. 书写js
'use strict';
// document.sandbox="allow-same-origin allow-popups allow-forms allow-scripts";var htmlcontent;
new QWebChannel(qt.webChannelTransport,
function(channel) {htmlcontent = channel.objects.context;
});
document.getElementById("checkresult").onchange=function(){var html = document.getElementsByTagName('html')[0].innerHTML;htmlcontent.fromWebPageToLocal(html);alert(html);
}

注意JS的放置顺序:浏览器自上而下解析,JS需要的标签 可能还不存在。建议在页面加载完成后再注入JS。

connect(m_WebView, &QWebEngineView::loadFinished, [=]() {m_WebView->page()->runJavaScript(bywebjs);});

完整代码

qputenv("QTWEBENGINE_REMOTE_DEBUGGING", "9223");// 设置页面大小
int iwebheight = QPageSize(QPageSize::A4).sizePoints().height();
m_WebView = new QWebEngineView();
m_WebView->setFixedHeight(iwebheight);
ui.gridLayout_content->addWidget(m_WebView);// 加载页面
m_WebView->load(QUrl::fromLocalFile(strHtml));
connect(m_WebView, &QWebEngineView::loadFinished, this, &ReportWidget::OnWebLoadFinish);// 注入js交互
void ReportWidget::OnWebLoadFinish(){QFile filewebjs(":/Resource/report/qwebchannel.js");QFile filereportjs(":/Resource/report/report.js");if(!filewebjs.open(QIODevice::ReadOnly) || !filereportjs.open(QIODevice::ReadOnly)) {return;}QByteArray bywebjs = filewebjs.readAll();QString strreportjs = filereportjs.readAll();bywebjs.append(strreportjs);//QWebEnginePage *myPage = new QWebEnginePage(profile, m_WebView);//m_WebView->setPage(myPage);//myPage->scripts().insert(script);// web channel 与js交互:页面到本地m_ReportChannel = new DReportChannelCmd(this);connect(m_ReportChannel, &DReportChannelCmd::SaveHtmlFile, ReportManage, &DReportManage::OnSaveFile);  // 保存html文件: 由Channel类负责交互m_WebChannel = new QWebChannel(this);m_WebChannel->registerObject("context", m_ReportChannel);m_WebView->page()->setWebChannel(m_WebChannel);m_WebView->page()->runJavaScript(bywebjs);// 本地到页面传参: 对应js中的函数m_WebView->page()->runJavaScript(QString("AddSnapshotList('%1', '%2')").arg(strimgpath).arg(strimgscale));
});

QWebChannel为注入脚本的依赖库,一起放到页面加载之后。否则bywebjs中的内容可能提前被调用,导致无法识别标签。

problem

无法加载Qt5WebEngineCore.pdb

VS release版本设置生成pdb调试文件
在这里插入图片描述

在这里插入图片描述

资料:
Qt实现网页与本地应用(QWebEngine应用)之间的通讯(QWebChannel的使用)
Qt嵌入浏览器(二)——QWebChannel实现与页面的通信
QWebChannel - js: Uncaught ReferenceError: qt is not defined
PyQt5 内嵌浏览器注入 Javascript 脚本实现自动化操作
Qt嵌入浏览器(一)——QWebEngineView实现浏览器基本功能
QT Webkit 自动提交表单
qt QWebEngineView 使用和截屏显示绘制输出


http://www.ppmy.cn/news/546311.html

相关文章

The web application [/XinRui02] created a ThreadLocal with key of type [com.opensymphony.xwork2.inje

项目在redeploy时出现以下异常&#xff1a; 2014-1-2 17:22:46 org.apache.catalina.loader.WebappClassLoader clearThreadLocalMap 严重: The web application [/XinRui02] created a ThreadLocal with key of type [com.opensymphony.xwork2.inject.ContainerImpl$10] (va…

web工程集成ckeditor/ckfinder实现在现编辑

一、 ckeditorckfinder相关资源 ckeditor官网&#xff1a;http://ckeditor.com/ ckeditor下载地址&#xff1a;http://ckeditor.com/download ckeditor4.1下载地址&#xff1a;http://download.cksource.com/CKEditor/CKEditor/CKEditor%204.1/ckeditor_4.1_standard.zip …

myeclipse下使用xfire,JAX-WS、Axis 创建web service入门实例

一、XFire XFire是一个免费的开源SOAP框架&#xff0c;它不仅可以极大方便地实现这样一个环境&#xff0c;并且可以提供许多Web Services规范中高级特征&#xff0c;这些特征在多数的商业或者开源工具都没有提供。http://xfire.codehaus.org/Download是它的官方网站下载区&…

用xfire的Eclipse Plugin生成web服务的客户端

本文编码环境&#xff1a; Eclipse SDK 3.2.2 &#xff0c; Tomcat5.5.20 &#xff0c; XFire-1.2.6 。 正文&#xff1a; 利用xfire生成web服务客户端的方法有多种&#xff0c;Eclipse Plugin为XFire的WSDL->Code generator提供了Eclipse支持&#xff0c;它需要Ec…

XLNet简介

背景知识 语言模型&#xff1a;自回归和自编码模式 图示&#xff1a; 黄色块为输入字符&#xff0c;蓝色块为字符的位置。 对于自回归语言模型&#xff0c;它希望通过已知的前半句预测后面的词或字。 对于自编码语言模型&#xff0c;它希望通过一句话预测被 Mask 掉的字或词&a…

应用XFire开发WebService实例 xfire+myeclipse+weblogic 8

1. XFire介绍 XFire和Axis都是新一代的java web服务框架。XFire提供了非常方便的API&#xff0c;使用这些API可以开发面向服务(SOA)的程序。他比axis性能要高&#xff0c;支持各种标准&#xff0c;性能优良&#xff0c;基于低内存的STAX模型&#xff0c;也是其于流的XML。 • 支…

Day8 xlrd和xlwt操作Excel文件

一、xlrd获取文件内容 import xlrd 1.打开Excel文件 wb xlrd.open_workbook(file/data1.xls) 2.获取所有工作表的表名 工作簿.sheet_names() names wb.sheet_names() print(names) 3.获取工作表 1)获取所有的工作表 - 工作簿.sheets() all_sheet wb.sheets() print(all_shee…

WinDLX实验:熟悉WinDLX

基本介绍 DLX是一个流水线处理器实例。 DLX以及一些机器的指令集结构的设计思想十分相似&#xff1a;具有一个简单Load/Store Load/Store指令集&#xff1b;注重指令的流水效率&#xff1b;简化指令的译码&#xff1b;高效支持编译器。 WinDLX 是一个基于Windows 的 DLX 模拟器…