| QJson project page | QJson home page |
Class used to convert QObject into QVariant and vivce-versa. During these operations only the class attributes defined as properties will be considered. More...
#include <qobjecthelper.h>
Static Public Member Functions | |
| static QVariantMap | qobject2qvariant (const QObject *object, const QStringList &ignoredProperties=QStringList(QString(QLatin1String("objectName")))) |
| static void | qvariant2qobject (const QVariantMap &variant, QObject *object) |
Class used to convert QObject into QVariant and vivce-versa. During these operations only the class attributes defined as properties will be considered.
Suppose the declaration of the Person class looks like this:
class Person : public QObject { Q_OBJECT Q_PROPERTY(QString name READ name WRITE setName) Q_PROPERTY(int phoneNumber READ phoneNumber WRITE setPhoneNumber) Q_PROPERTY(Gender gender READ gender WRITE setGender) Q_PROPERTY(QDate dob READ dob WRITE setDob) Q_ENUMS(Gender) public: Person(QObject* parent = 0); ~Person(); QString name() const; void setName(const QString& name); int phoneNumber() const; void setPhoneNumber(const int phoneNumber); enum Gender {Male, Female}; void setGender(Gender gender); Gender gender() const; QDate dob() const; void setDob(const QDate& dob); private: QString m_name; int m_phoneNumber; Gender m_gender; QDate m_dob; };
The following code will serialize an instance of Person to JSON :
Person person;
person.setName("Flavio");
person.setPhoneNumber(123456);
person.setGender(Person::Male);
person.setDob(QDate(1982, 7, 12));
QVariantMap variant = QObjectHelper::qobject2qvariant(&person);
Serializer serializer;
qDebug() << serializer.serialize( variant);
The generated output will be:
{ "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }
It's also possible to initialize a QObject using the values stored inside of a QVariantMap.
Suppose you have the following JSON data stored into a QString:
{ "dob" : "1982-07-12", "gender" : 0, "name" : "Flavio", "phoneNumber" : 123456 }
The following code will initialize an already allocated instance of Person using the JSON values:
Parser parser;
QVariant variant = parser.parse(json);
Person person;
QObjectHelper::qvariant2qobject(variant.toMap(), &person);
Definition at line 115 of file qobjecthelper.h.
| QVariantMap QObjectHelper::qobject2qvariant | ( | const QObject * | object, | |
| const QStringList & | ignoredProperties = QStringList(QString(QLatin1String("objectName"))) | |||
| ) | [static] |
This method converts a QObject instance into a QVariantMap.
| object | The QObject instance to be converted. | |
| ignoredProperties | Properties that won't be converted. |
Definition at line 44 of file qobjecthelper.cpp.
| void QObjectHelper::qvariant2qobject | ( | const QVariantMap & | variant, | |
| QObject * | object | |||
| ) | [static] |
This method converts a QVariantMap instance into a QObject
| object | The QObject instance to be converted. |
Definition at line 63 of file qobjecthelper.cpp.
|
|
hosts this site. |
Send comments to: QJson Developers |