从QtWebKit迁移到QtWebEngine,使用QWebChannel。
我有一个可调用的函数将QVariant对象发送到Javascript,它被视为JSON对象。因此,QString
变成了string
,QInt
变成了int
等。
在没有QWebChannel的情况下使用QtWebKit,QByteArray被视为Uint8ClampedArray
,但现在直接使用UTF-8转换为string
(我的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 the signal
channel.objects.foo._nativeToJs.connect(function(msg){
//msg is now a JSON object
});
});
msg.result
应该包含一个夹紧的数组(msgpack数据),我稍后会对其进行解码。现在我有一个丑陋的 字符串
,其中包含了不是UTF-8字符但被解释为UTF-8,我无法使用它。