久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何使用 Qt WebEngine 和 QWebChannel?

How to use Qt WebEngine and QWebChannel?(如何使用 Qt WebEngine 和 QWebChannel?)
本文介紹了如何使用 Qt WebEngine 和 QWebChannel?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在使用新的 WebEngine 來玩耍和學(xué)習(xí).我一直試圖找到一些使用 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()

我發(fā)現(xiàn)使用 Qt WebEngine 時,必須使用 QWebChannel 將函數(shù)注冊到 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.

我已經(jīng)在我的電腦上安裝了 Qt 5.4.0.我注意到 qwebchannel.js 在我電腦上安裝的 SDK 中沒有找到.我在 Git 源碼上找到的.

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.

如果我有一個帶有 QWebEnginePageQWebEngineView 的 Qt 本機(jī)桌面應(yīng)用程序,我需要什么才能在 JavaScript 窗口對象上注冊函數(shù)?

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?

我的桌面應(yīng)用程序會自動導(dǎo)航到我創(chuàng)建的 http 頁面.所以我可以訪問連接到 QWebEngineView 的內(nèi)容.

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

需要采取哪些步驟才能完成這項工作?

What are the steps to take so I can make this work?

推薦答案

在 Qt5.6 中,如果想讓 C++ 部分和 JavaScript 進(jìn)行通信,唯一的方法就是使用 QWebChannel 在 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 端可用.現(xiàn)在,這是在 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;
        });

現(xiàn)在,如果你想在 JS 端檢索類的一些屬性,你需要在 C++ 端有一個方法,它返回一個字符串,一個整數(shù),一個長......這是它的樣子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);

現(xiàn)在,你在 JS 端得到這樣的 int :

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

var myIntInJSside= JSobject.myIntInCppSide;

這是一個非常簡單的解釋,我建議您觀看這個視頻對我很有用.此外,您可能想了解更多關(guān)于 QWebChannel 提供的 JavaScript API,以及有關(guān) 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.

希望有幫助!

這篇關(guān)于如何使用 Qt WebEngine 和 QWebChannel?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 国产一区二区电影 | 亚洲成人免费 | 日韩在线一区二区 | 久久久久国产精品一区二区 | 欧美午夜视频 | 久久久久久久一区 | k8久久久一区二区三区 | 国产精品一区久久久 | 日韩中文字幕视频在线观看 | 神马久久春色视频 | 日韩精品成人一区二区三区视频 | 亚洲一区精品在线 | av中文字幕网 | 日韩精品视频在线 | 九九热这里只有精品在线观看 | 操视频网站 | 日本在线视频一区二区 | 久久久99国产精品免费 | 日韩一区二区三区精品 | 91资源在线观看 | 国产成人精品一区二 | 一区二区三区电影在线观看 | av一级一片 | 久久99精品久久久久 | 日韩毛片在线视频 | 日韩黄| 中文字幕一区二区三区乱码在线 | 精品美女视频在免费观看 | 国产伦精品一区二区 | av在线一区二区三区 | 91天堂网 | 久久精品黄色 | 日本一区二区高清视频 | 亚洲日韩中文字幕一区 | 国产中文视频 | 在线日韩中文字幕 | 欧美精品一二三 | 在线免费91| 久久精品国产99国产精品亚洲 | 五月精品视频 | 99热热99 |