我使用QWebChannel在Qt中实现了一个HTML包装器,并且我成功地能够发送字符串,但是,我希望发送一个QJsonObject,而不是像"{a:1,b:2}“这样的json字符串,而是一个Qt QJsonObject。有可能吗?
官方文件上说
“不需要手动传递和序列化数据,”http://doc.qt.io/qt-5/qwebchannel.html
如何用JsonObject而不是字符串发出信号?
这是我的QWebChannel连接类
class Mapa : public QObject{
Q_OBJECT
public:
explicit Mapa();
displayMessage(const QString &message);
signals:
updateText(const QString &text); // success :sends text
updateJson( const QJsonObject &json); // fail: sends null
updateJsond(const QJsonDocument &jsondoc);// fail: sends null
}
}这是我的主要代码
Mapa map;
// setup the channel
QWebChannel channel;
QObject::connect(&clientWrapper, &WebSocketClientWrapper::clientConnected, &channel, &QWebChannel::connectTo);
// setup the dialog and publish it to the QWebChannel
channel.registerObject(QStringLiteral("map"), &map);
map.updateText("text");// sends "text" string
QJsonObject j;
j["Altitude"] = 10;
map.updateJson(j); // sends "null" string
QJsonDocument doc(j);
map.updateJsond(doc); // sends "null" string发布于 2016-03-03 16:15:51
与使用QJson系列对象不同,您可以将QVariant对象发送到Javascript代码
QJsonObject = QVariantMapQJsonArray= QVariantList您可以很容易地使用.toVariantMap()和.toVariantList()方法从JSON对象转换对象。
https://stackoverflow.com/questions/34901458
复制相似问题