Serializing QList<Custom> to JSON data Qt



  • Hi I am trying to convert the object into JSON. In that process i have created 2 classes.
    @
    class class1 : Public object
    {
    Q_OBJECT

    Q_PROPERTY(QString Item1 READ getItem1 WRITE setItem1)
    Q_PROPERTY(qint64 Item2 READ getItem2 WRITE setItem2)
    

    private:

    QString Item1;
    qint64 Item2;
    

    public:

    QString getItem1 () const { return Item1;}
    
    void setItem1 (const QString& Item){ Item1= Item; }
    

    // In the same way i have it for item2

    }

    Q_DECLARE_METATYPE(class1*);

    Class class2 : public QObject
    {

    Q_OBJECT
    
    Q_PROPERTY(QString Item1 READ getItem1 WRITE setItem1)
    Q_PROPERTY(qint64 Item2 READ getItem2 WRITE setItem2)
    Q_PROPERTY(QList<Class1*> class1List READ getclass1List WRITE setclass1List)
    

    private:
    qint64 Item5;
    qint64 Item6;
    QList< class1* > class1List;

    public:

    QString getItem5 () const { return Item5;}
    
    void setItem5 (const QString& Item){ Item5 = Item; }
    
    QString getItem6 () const { return Item6;}
    
    void setItem6 (const QString& Item){ Item6 = Item; }
    
    const QList<class1*> &getclass1List() const { return class1List;}
    
    void setclass1List(const QList<class1List*> &List) { class1List= List; }
    

    @
    Now i have converted the class2 object which has class1 list into QVariant using QJson::QObjectHelper::qobject2qvariant, All the data inside class2 is converted to JSON, but the list is printed as NULL.

    Please let me know what is wrong in this procedure.

    I have also tried it with
    @
    QList<class1*> getclass1List() const { return class1List;}

    void setclass1List(const QList<class1List*> List) { class1Lis = List; }
    @

    But no Use... i am still getting NULL for the value of list in JSON object. If i print the same list as a separate object, i am getting the data.

    [Added @ around code. andreyc]


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.