diff --git a/core/networkpackage.cpp b/core/networkpackage.cpp index 64cfab764..eed65b232 100644 --- a/core/networkpackage.cpp +++ b/core/networkpackage.cpp @@ -65,13 +65,14 @@ void NetworkPackage::createIdentityPackage(NetworkPackage* np) //qCDebug(KDECONNECT_CORE) << "createIdentityPackage" << np->serialize(); } -QVariantMap qobject2qvariant(const QObject* object) +template +QVariantMap qobject2qvariant(const T* object) { QVariantMap map; - auto metaObject = object->metaObject(); - for(int i = metaObject->propertyOffset(); i < metaObject->propertyCount(); ++i) { - const char *name = metaObject->property(i).name(); - map.insert(QString::fromLatin1(name), object->property(name)); + auto metaObject = T::staticMetaObject; + for(int i = metaObject.propertyOffset(); i < metaObject.propertyCount(); ++i) { + QMetaProperty prop = metaObject.property(i); + map.insert(QString::fromLatin1(prop.name()), prop.readOnGadget(object)); } return map; @@ -107,18 +108,19 @@ QByteArray NetworkPackage::serialize() const return json; } -void qvariant2qobject(const QVariantMap& variant, QObject* object) +template +void qvariant2qobject(const QVariantMap& variant, T* object) { for ( QVariantMap::const_iterator iter = variant.begin(); iter != variant.end(); ++iter ) { - const int propertyIndex = object->metaObject()->indexOfProperty(iter.key().toLatin1()); + const int propertyIndex = T::staticMetaObject.indexOfProperty(iter.key().toLatin1()); if (propertyIndex < 0) { qCWarning(KDECONNECT_CORE) << "missing property" << object << iter.key(); continue; } - QMetaProperty property = object->metaObject()->property(propertyIndex); - bool ret = property.write(object, *iter); + QMetaProperty property = T::staticMetaObject.property(propertyIndex); + bool ret = property.writeOnGadget(object, *iter); if (!ret) { qCWarning(KDECONNECT_CORE) << "couldn't set" << object << "->" << property.name() << '=' << *iter; } diff --git a/core/networkpackage.h b/core/networkpackage.h index 6a0bf9c91..909bb6ea6 100644 --- a/core/networkpackage.h +++ b/core/networkpackage.h @@ -37,10 +37,9 @@ class FileTransferJob; -// TODO: investigate if we can turn into Q_GADGET, drop QObject -class KDECONNECTCORE_EXPORT NetworkPackage : public QObject +class KDECONNECTCORE_EXPORT NetworkPackage { - Q_OBJECT + Q_GADGET Q_PROPERTY( QString id READ id WRITE setId ) Q_PROPERTY( QString type READ type WRITE setType ) Q_PROPERTY( QVariantMap body READ body WRITE setBody )