使用QWebChannel从QtWebKit迁移到QtWebEngine.
我有一个可调用的函数,它将QVariant对象发送到Javascript,它被视为JSON对象.因此,一个QString变成了string,一个QInt一个int,等等.
在没有QWebChannel的情况下使用QtWebKit,QByteArray被视为a Uint8ClampedArray,但现在直接转换为string使用UTF-8(我的QByteArray不是:())
我做错什么了吗 ?我该怎么办 ?
这是相关的代码部分:
//Qt Window class signal to javascript
void MyWindow::uplink(Response msg)
{
emit _nativeToJs(msg->toJson());
}
//Response class toJson() method
QVariantMap Response::toJson() const
{
QVariantMap map;
map["id"] = m_id; //qulonglong
map["src"] = QString(m_src);
map["dst"] = QString(m_dst);
map["status"] = m_status; //qint16
map["result"] = m_result; //QVariant, can be a map of string, arrays, etc
return map;
}
//Javascript
var foo;
new QWebChannel(qt.webChannelTransport, function(channel) {
//we connect …Run Code Online (Sandbox Code Playgroud) 我有一个Qt应用程序,用于启动Webview QWebChannel.
在这些视图中,我有JavaScript做一些事情,以根据屏幕大小定位/调整窗口大小.该screen对象应该提供这种信息.(屏幕文档)
但在我看来QWebEnginePage,在整个加载过程中屏幕对象都是空的.
如果我在一个按钮上放置一个监听器来到screen.height页面上的某个位置,现在它可以工作了.
//js local file called in html head
//screen = {}
document.addEventListener("load", function(event) {
//screen = {}
new QWebChannel(qt.webChannelTransport, function(channel) {
//screen = {}
//stuff
channel.object.myClass.show(function(res){ //Qt function that calls the show() method of the QWebEngineView
//screen = {}
});
});
document.getElementById("myBtn").addEventListener("click", function(){
console.log("height:" + screen.height); // screen: 1440
});
});
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,如何screen在JavaScript中的某个时刻访问值?