如何设置QWebChannel JS API以在QWebEngineView中使用? [英] How to setup QWebChannel JS API for use in a QWebEngineView?

查看:522
本文介绍了如何设置QWebChannel JS API以在QWebEngineView中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于Qt的文档还远远不够完善,因此这是设置QWebChannel的方法,与建议在HTML页面中添加< script> 标记的方法有所不同在文档中?

As Qt's documentation is far from complete, which are the ways to setup QWebChannel and are there different ones from adding a <script> tag in your HTML page like recommended in the documentation?

推荐答案

有不同的选项来加载所需的脚本,该脚本可通过URL qrc访问: /// qtwebchannel / qwebchannel.js

There are different options to load the needed script accessible at the URL qrc:///qtwebchannel/qwebchannel.js:

注意:


  • 仅当您从QWebEngineView中查看页面时才能使用该URL。否则,您可能需要将API文件加载到Web服务器。

先决条件:


  • 在您的 .pro QT + =网络频道 c>文件并运行 qmake

  • add QT += webchannel to your .pro file and run qmake

使用 runJavaScript()执行Qt的QWebChannel JS API中的代码:

Using runJavaScript() to execute the code in Qt's QWebChannel JS API:

我喜欢的方法是因为它看起来很简单并且具有到目前为止是可靠的。
请记住,由于 runJavaScript()方法不会阻塞当前线程,因此在此代码完成之前可能无法设置API。

My prefered method because it seems simple and has been reliable so far. Keep in mind the possibility that the API might not be set up by the time this code has finished as the runJavaScript() method does not block the current thread until the JS code has been executed.

QFile apiFile(":/qtwebchannel/qwebchannel.js"); //load the API from the resources
if(!apiFile.open(QIODevice::ReadOnly))
    qDebug()<<"Couldn't load Qt's QWebChannel API!";
QString apiScript = QString::fromLatin1(apiFile.readAll());
apiFile.close();
QWebEngineView view;    //your custom QWebEngineView
view.page().runJavaScript(apiScript);

使用 runJavaScript()到执行代码以加载Qt的QWebChannel JS API:

Using runJavaScript() to execute code to load Qt's QWebChannel JS API:

尽管我没有尝试过这些方法,但它们仍然可以工作,因为它们旨在加载外部JS文件。
建议一种方法之一此处可能会派上用场,具体取决于您使用的其他库。

Although I didn't try these methods they should still work, as they aim to load external JS files. One of the methods suggested here might come in handy depending on which other libraries you use.

使用< script> 网页中的标记:

Using a <script> tag in the web page:

这是Qt文档推荐的默认方法。它具有在页面加载后立即提供API的优势。
这样做的缺点是您需要修改网页的HTML,如果您不是该网站的所有者,则可能会很痛苦。

This is the default method recommended by Qt's documentation. It has the advantage of providing the API immediatly after the page was loaded. The downside of this is that you need to modify the HTML of the web page which can be painful if you are not the owner of the site.

不要忘记在C ++端创建一个QWebChannel对象:

Don't forget create a QWebChannel object on the C++ side:

QWebChannel* channel_ = new QWebChannel(view.page());
//attach it to the QWebEnginePage
view.page()->setWebChannel(channel_);
//register a QObject to be exposed to JavaScript
channel_->registerObject(QStringLiteral("jshelper"), this);

在JavaScript代码上设置QWebChannel:

如注释中所指出,为了在C ++和JavaScript之间共享对象,您需要初始化QWebChannel。

As pointed out in the comments in order to share objects between C++ and JavaScript you need to initialize the QWebChannel.

new QWebChannel(qt.webChannelTransport, function (channel) {
    var sharedObject = channel.objects.sharedObject;
});






此答案被认为是Qt文档的补充,因此,如果您想添加任何东西,请随时这样做。


This answer was thought as an addition to Qt's documentation, so if you want to add something feel free to do so.

版本:Qt 5.6.1

Version: Qt 5.6.1

来源:

  • QT QWebEnginePage::setWebChannel() transport object
  • How do I include a JavaScript file in another JavaScript file?
  • How to use Qt WebEngine and QWebChannel?
  • http://doc.qt.io/qt-5/qtwebchannel-javascript.html

这篇关于如何设置QWebChannel JS API以在QWebEngineView中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆