From 21204971ffd768fa305f941b5a94822f91d74844 Mon Sep 17 00:00:00 2001 From: Francois Ferrand Date: Thu, 11 Apr 2013 20:42:00 +0200 Subject: [PATCH] re-apply: Cleaner qobject2qvariant(). --- src/http/qjson/src/qobjecthelper.cpp | 13 +++++++++++-- src/http/qjson/src/qobjecthelper.h | 14 ++++++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/http/qjson/src/qobjecthelper.cpp b/src/http/qjson/src/qobjecthelper.cpp index 4f8daf722..0ea440522 100644 --- a/src/http/qjson/src/qobjecthelper.cpp +++ b/src/http/qjson/src/qobjecthelper.cpp @@ -41,8 +41,8 @@ QObjectHelper::~QObjectHelper() delete d; } -QVariantMap QObjectHelper::qobject2qvariant( const QObject* object, - const QStringList& ignoredProperties) +QVariantMap QObjectHelper::qobject2qvariant(const QObject* object, Flags flags, + const QStringList& ignoredProperties) { QVariantMap result; const QMetaObject *metaobject = object->metaObject(); @@ -55,11 +55,20 @@ QVariantMap QObjectHelper::qobject2qvariant( const QObject* object, continue; QVariant value = object->property(name); + if (value.isNull() && !flags.testFlag(Flag_StoreNullVariants)) + continue; + if (!value.isValid() && !flags.testFlag(Flag_StoreInvalidVariants)) + continue; result[QLatin1String(name)] = value; } return result; } +QVariantMap QObjectHelper::qobject2qvariant(const QObject *object, const QStringList &ignoredProperties) + { + return qobject2qvariant(object, Flag_All, ignoredProperties); + } + void QObjectHelper::qvariant2qobject(const QVariantMap& variant, QObject* object) { const QMetaObject *metaobject = object->metaObject(); diff --git a/src/http/qjson/src/qobjecthelper.h b/src/http/qjson/src/qobjecthelper.h index e4dfed0c8..9a819b448 100644 --- a/src/http/qjson/src/qobjecthelper.h +++ b/src/http/qjson/src/qobjecthelper.h @@ -120,14 +120,24 @@ namespace QJson { QObjectHelper(); ~QObjectHelper(); + enum Flag { + Flag_None, + Flag_StoreNullVariants, + Flag_StoreInvalidVariants, + Flag_All = Flag_StoreNullVariants | Flag_StoreInvalidVariants + }; + Q_DECLARE_FLAGS(Flags, Flag) + /** * This method converts a QObject instance into a QVariantMap. * * @param object The QObject instance to be converted. * @param ignoredProperties Properties that won't be converted. */ - static QVariantMap qobject2qvariant( const QObject* object, - const QStringList& ignoredProperties = QStringList(QString(QLatin1String("objectName")))); + static QVariantMap qobject2qvariant(const QObject* object, Flags flags = Flag_All, + const QStringList& ignoredProperties = QStringList(QString(QLatin1String("objectName")))); + static QVariantMap qobject2qvariant(const QObject* object, + const QStringList& ignoredProperties); /** * This method converts a QVariantMap instance into a QObject