【问题标题】:How send a QJsonObject using QWebChannel in Qt如何在 Qt 中使用 QWebChannel 发送 QJsonObject
【发布时间】:2016-01-20 13:32:28
【问题描述】:

我正在使用 QWebChannel 在 Qt 中实现一个 HTML 包装器,并且我能够成功发送字符串,但是,我想发送一个 QJsonObject,而不是像“{a:1,b:2}”这样的 json 字符串,而是一个 Qt QJson 对象。是否可以?

官方文档说

“不需要手动消息传递和数据序列化,” 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

【问题讨论】:

    标签: c++ json qt qwebview qjsonobject


    【解决方案1】:

    您可以将QVariant 对象发送到您的Javascript 代码,而不是使用QJson 系列对象

    • QJsonObject = QVariantMap
    • QJsonArray= QVariantList

    您可以使用.toVariantMap().toVariantList() 方法轻松地从JSON 对象转换您的对象。

    【讨论】:

      猜你喜欢
      • 2015-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      • 2015-07-29
      • 1970-01-01
      相关资源
      最近更新 更多