diff options
author | Milian Wolff <milian.wolff@kdab.com> | 2016-05-25 16:42:01 +0200 |
---|---|---|
committer | Milian Wolff <milian.wolff@kdab.com> | 2016-05-25 17:06:15 +0000 |
commit | 97c876a1353f29ed0129360f013f2529bed69d98 (patch) | |
tree | af2671db0f408bc88a4fe950a5bbfdcf8a6e3811 /src/webchannel | |
parent | cc0ed84fa6eac8119313543d2dc33658a78e50c7 (diff) | |
download | qtwebchannel-97c876a1353f29ed0129360f013f2529bed69d98.tar.gz |
Enable calling C++ functions taking QJson arguments via webchannel.
We used to convert the QJsonValue arguments to QVariants, which
then failed to call a C++ function which expected on of the three
QJson data types, i.e. QJsonValue, QJsonObject or QJsonArray. Instead,
we now detect these three cases and manually convert the QJsonValue
as needed.
[ChangeLog] C++ functions taking arguments of type QJsonValue,
QJsonArray or QJsonObject can now be called via the Qt WebChannel.
Change-Id: I94e0c8937ca35e2ecd3554f7ddf2d4e5a3328570
Task-number: QTBUG-48198
Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
Diffstat (limited to 'src/webchannel')
-rw-r--r-- | src/webchannel/qmetaobjectpublisher.cpp | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/src/webchannel/qmetaobjectpublisher.cpp b/src/webchannel/qmetaobjectpublisher.cpp index 324191e..e85bc76 100644 --- a/src/webchannel/qmetaobjectpublisher.cpp +++ b/src/webchannel/qmetaobjectpublisher.cpp @@ -87,6 +87,29 @@ QJsonObject createResponse(const QJsonValue &id, const QJsonValue &data) /// TODO: what is the proper value here? const int PROPERTY_UPDATE_INTERVAL = 50; + +QVariant toVariant(const QJsonValue &value, int targetType) +{ + if (targetType == QMetaType::QJsonValue) { + return QVariant::fromValue(value); + } else if (targetType == QMetaType::QJsonArray) { + if (!value.isArray()) + qWarning() << "Cannot not convert non-array argument" << value << "to QJsonArray."; + return QVariant::fromValue(value.toArray()); + } else if (targetType == QMetaType::QJsonObject) { + if (!value.isObject()) + qWarning() << "Cannot not convert non-object argument" << value << "to QJsonObject."; + return QVariant::fromValue(value.toObject()); + } + + // this converts QJsonObjects to QVariantMaps, which is not desired when + // we want to get a QJsonObject or QJsonValue (see above) + QVariant variant = value.toVariant(); + if (targetType != QMetaType::QVariant && !variant.convert(targetType)) { + qWarning() << "Could not convert argument" << value << "to target type" << QVariant::typeToName(targetType) << '.'; + } + return variant; +} } QMetaObjectPublisher::QMetaObjectPublisher(QWebChannel *webChannel) @@ -366,11 +389,7 @@ QVariant QMetaObjectPublisher::invokeMethod(QObject *const object, const int met // construct converter objects of QVariant to QGenericArgument VariantArgument arguments[10]; for (int i = 0; i < qMin(args.size(), method.parameterCount()); ++i) { - QVariant arg = args.at(i).toVariant(); - if (method.parameterType(i) != QMetaType::QVariant && !arg.convert(method.parameterType(i))) { - qWarning() << "Could not convert argument" << args.at(i) << "to target type" << method.parameterTypes().at(i) << '.'; - } - arguments[i].value = arg; + arguments[i].value = toVariant(args.at(i), method.parameterType(i)); } // construct QGenericReturnArgument |