From 97c876a1353f29ed0129360f013f2529bed69d98 Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Wed, 25 May 2016 16:42:01 +0200 Subject: 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 --- src/webchannel/qmetaobjectpublisher.cpp | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) (limited to 'src/webchannel/qmetaobjectpublisher.cpp') 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 -- cgit v1.2.1 From bec50124b893c4632829d9806f49f64c4debf936 Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Wed, 25 May 2016 18:20:30 +0200 Subject: Fix setting properties of QJson{Value,Array,Object} type. Similar to the previous issue, where these types were not properly converted to QVariant when invoking a method, we manually do the conversion now to get the desired behavior. The culprit is again that QJsonValue::toVariant converts an object e.g. to a QVariantMap, and not to a QVariant containing a QJsonObject. [ChangeLog] QObject properties of type QJsonValue, QJsonArray or QJsonObject can now be set via the Qt WebChannel. Task-number: QTBUG-48198 Change-Id: I5d574b1a5cffd6d6ad9b555f2a3e872b9c3425a7 Reviewed-by: Simon Hausmann --- src/webchannel/qmetaobjectpublisher.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'src/webchannel/qmetaobjectpublisher.cpp') diff --git a/src/webchannel/qmetaobjectpublisher.cpp b/src/webchannel/qmetaobjectpublisher.cpp index e85bc76..0fa4ae2 100644 --- a/src/webchannel/qmetaobjectpublisher.cpp +++ b/src/webchannel/qmetaobjectpublisher.cpp @@ -410,6 +410,16 @@ QVariant QMetaObjectPublisher::invokeMethod(QObject *const object, const int met return returnValue; } +void QMetaObjectPublisher::setProperty(QObject *object, const int propertyIndex, const QJsonValue &value) +{ + QMetaProperty property = object->metaObject()->property(propertyIndex); + if (!property.isValid()) { + qWarning() << "Cannot set unknown property" << propertyIndex << "of object" << object; + } else if (!property.write(object, toVariant(value, property.userType()))) { + qWarning() << "Could not write value " << value << "to property" << property.name() << "of object" << object; + } +} + void QMetaObjectPublisher::signalEmitted(const QObject *object, const int signalIndex, const QVariantList &arguments) { if (!webChannel || webChannel->d_func()->transports.isEmpty()) { @@ -607,14 +617,8 @@ void QMetaObjectPublisher::handleMessage(const QJsonObject &message, QWebChannel } else if (type == TypeDisconnectFromSignal) { signalHandler.disconnectFrom(object, message.value(KEY_SIGNAL).toInt(-1)); } else if (type == TypeSetProperty) { - const int propertyIdx = message.value(KEY_PROPERTY).toInt(-1); - QMetaProperty property = object->metaObject()->property(propertyIdx); - if (!property.isValid()) { - qWarning() << "Cannot set unknown property" << message.value(KEY_PROPERTY) << "of object" << objectName; - } else if (!object->metaObject()->property(propertyIdx).write(object, message.value(KEY_VALUE).toVariant())) { - qWarning() << "Could not write value " << message.value(KEY_VALUE) - << "to property" << property.name() << "of object" << objectName; - } + setProperty(object, message.value(KEY_PROPERTY).toInt(-1), + message.value(KEY_VALUE)); } } } -- cgit v1.2.1 From f48e8c9711fbeb350ccf70f852ce3732844d4287 Mon Sep 17 00:00:00 2001 From: Kai Dohmen Date: Mon, 11 Apr 2016 19:38:50 +0200 Subject: Make passing objects from website to server possible If you get an object from the server and want to pass it back to the server via a function the id of the object is passed instead of the whole json object. On the server side QMetaObjectPublisher::invokeMethod now looks up the object in QMetaObjectPublisher::wrappedObjects by the passed object-id. Task-number: QTBUG-50075 Change-Id: Id0df2dfaa79bcba12ca48391ae7537ac1a086898 Reviewed-by: Milian Wolff --- src/webchannel/qmetaobjectpublisher.cpp | 63 +++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 23 deletions(-) (limited to 'src/webchannel/qmetaobjectpublisher.cpp') diff --git a/src/webchannel/qmetaobjectpublisher.cpp b/src/webchannel/qmetaobjectpublisher.cpp index 0fa4ae2..b3fc53d 100644 --- a/src/webchannel/qmetaobjectpublisher.cpp +++ b/src/webchannel/qmetaobjectpublisher.cpp @@ -87,29 +87,6 @@ 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) @@ -472,6 +449,46 @@ void QMetaObjectPublisher::objectDestroyed(const QObject *object) pendingPropertyUpdates.remove(object); } +QObject *QMetaObjectPublisher::unwrapObject(const QString &objectId) const +{ + if (!objectId.isEmpty()) { + ObjectInfo objectInfo = wrappedObjects.value(objectId); + if (objectInfo.object && !objectInfo.classinfo.isEmpty()) + return objectInfo.object; + } + + qWarning() << "No wrapped object" << objectId; + return Q_NULLPTR; +} + +QVariant QMetaObjectPublisher::toVariant(const QJsonValue &value, int targetType) const +{ + 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()); + } else if (QMetaType::typeFlags(targetType) & QMetaType::PointerToQObject) { + QObject *unwrappedObject = unwrapObject(value.toObject()[KEY_ID].toString()); + if (unwrappedObject == Q_NULLPTR) + qWarning() << "Cannot not convert non-object argument" << value << "to QObject*."; + return QVariant::fromValue(unwrappedObject); + } + + // 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; +} + // NOTE: transport can be a nullptr // in such a case, we need to ensure that the property is registered to // the target transports of the parentObjectId -- cgit v1.2.1