如何使用Qt WebEngine和QWebChannel? [英] How to use Qt WebEngine and QWebChannel?

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

问题描述

我使用新的WebEngine来玩耍和学习。
我一直在尝试找到一些类似的方法找到使用Qt WebKit: addToJavaScriptWindowObject()

I'm using the new WebEngine to play around and learn. I've been trying to find some similar methods found using Qt WebKit: addToJavaScriptWindowObject()

我发现使用Qt WebEngine,我必须使用 QWebChannel 来注册函数到Javascript窗口对象。如果这是正确的,它需要我到以下问题。

I found that using Qt WebEngine, I have to use the QWebChannel to register functions to the Javascript window object. If this is correct, it takes me to the following question.

我在我的电脑上安装了Qt 5.4.0。我注意到在我的计算机上安装的SDK中找不到 qwebchannel.js

I've installed Qt 5.4.0 on my computer. I noticed that qwebchannel.js is not found in the SDK installed on my computer. I found it on the Git source.

如果我有一个Qt本地桌面应用程序,并且 QWebEnginePage QWebEngineView ,我需要能够在Javascript窗口对象上注册函数?

If I have a Qt native desktop application with a QWebEnginePage and QWebEngineView, what do I need to be able to register functions on the Javascript window object?

我的桌面应用程序会自动导航我创建的http页面。所以我可以访问连接到 QWebEngineView 的内容。

My desktop application navigates automatically to a http page that I have created. So I have access to the content connected to the QWebEngineView.

这个工作?

推荐答案

在Qt5.6中,如果你想让C ++的部分和JavaScript沟通,它在 QWebChannel 。 qt.io/qt-5/qwebengineview.htmlrel =nofollow> QWebEngineView ,正如你所说。你在 .cpp 文件中这样做:

In Qt5.6, if you want to make C++ part and JavaScript to communicate, the only way to do it is using QWebChannel on a QWebEngineView, as you stated. You do it this way in the .cpp file:

m_pView = new QWebEngineView(this);
QWebChannel * channel = new QWebChannel(page);
m_pView->page()->setWebChannel(channel);
channel->registerObject(QString("TheNameOfTheObjectUsed"), this);

在这里,你只需要注册一个名为 TheNameOfTheObjectUsed 将在JS端提供。现在,这是在JS端使用的代码的一部分:

Here, you just say that you register an object named TheNameOfTheObjectUsed that will be available on the JS side. Now, this is the part of code to use in the JS side :

new QWebChannel(qt.webChannelTransport, function (channel) {
            // now you retrieve your object
            var JSobject = channel.objects.TheNameOfTheObjectUsed;
        });

现在,如果要在JS端检索类的某些属性,一个方法在C ++端返回一个字符串,一个整数,一个长...这是在C ++的一面,在你的 .h



Now, if you want to retrieve some properties of the class in the JS side, you need to have a method on the C++ side which returns a string, an integer, a long... This is what it looks like on the C++ side, in your .h:

Q_INVOKABLE int getInt();
Q_PROPERTY(int myIntInCppSide READ getInt);

现在,你在JS端得到这样的int:

And now, you get the int like this on the JS side :

var myIntInJSside= JSobject.myIntInCppSide;

这是一个非常简单的解释,建议您观看此视频,这对我非常有用。此外,您可能想要详细了解QWebChannel提供的 JavaScript API ,以及有关 QWebChannel 的文档;

This is a very simple explanation, and I recommend you to watch this video which was very useful to me. Also, you might want to read more about the JavaScript API provided by QWebChannel, as well as the documentation about QWebChannel;

希望有所帮助!

这篇关于如何使用Qt WebEngine和QWebChannel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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