如何在Qt中使用QWebChannel发送QJsonObject
How send a QJsonObject using QWebChannel in Qt
我使用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
您可以将QVariant
对象发送到 Javascript 代码中,而不是使用 QJson
系列对象
-
QJsonObject
=QVariantMap
-
QJsonArray
=QVariantList
您可以使用 .toVariantMap()
和.toVariantList()
方法轻松地从 JSON 对象转换对象。
相关文章:
- 从命令行c++发送文本文件名
- 发送一个带有libcurl C++问题的帖子请求:s
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 如何在C++中将函数发送到另一个进程
- boost xml parsingl将xml的路径作为变量发送
- 如何将数组从Objective-C发送到C++函数
- 如何使用ESP8266向谷歌主页发送命令
- 当服务中的事件被触发时,如何将响应从服务发送回客户端?
- 为什么默认复制函数在按值发送参数时不调用?
- 程序不断发送垃圾邮件号码?
- 如何使用 OpenCV 解码在两个 UWP 应用之间发送的图像字节?
- 如何使用 Google Test 向测试添加元数据 / 如何将数据从 Google Test 发送到 TestEven
- 如何接受 [ENTER] 键作为无效输入并发送错误消息
- Qt:如何使不兼容的发送方/接收方参数兼容?
- (Winsock) UDP 接收工作正常,但同一套接字的发送失败
- MSG_WAITALL的套接字发送得到了 22 EINVAL
- Win32 发送输入鼠标移动滞后并冻结
- 使用 Winsock2.h C++向不和谐 API 发送 HTTP 请求时出现问题
- 如何使用发送数据包所花费的时间计算两个节点之间的距离?
- 如何在Qt中使用QWebChannel发送QJsonObject