作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我使用 QWebChannel 在 Qt 中实现了一个 HTML 包装器,并且我能够成功地发送字符串,但是,我想发送一个 QJsonObject,而不是像“{a:1,b:2}”这样的 json 字符串,而是一个 Qt QJson对象。是否可以?
官方文档说
"No manual message passing and serialization of data is required," 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++ - 如何在 Qt 中使用 QWebChannel 发送 QJsonObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34901458/
我是一名优秀的程序员,十分优秀!