QVariantMap MainWindow::toMap(QObject obj){ QVariantMap map; for(int i=0; i<obj.metaObject()->propertyCount(); ++i) { if(obj.metaObject()->property(i).isStored(&obj)) { QVariant var = obj.metaObject()->property(i).read(&obj);---Тут QVariant map.insert(???,var); } } return map;}
#ifndef GPPPACKET_H#define GPPPACKET_H#include <QObject>class GPPPacket : public QObject{ Q_OBJECT Q_PROPERTY(QString head READ getHead WRITE setHead)private: QString head;public: explicit GPPPacket(QObject *parent = 0); void setHead(QString head){this->head=head;} QString getHead(){return head;}signals:public slots:};#endif // GPPPACKET_H
#ifndef SCREENPREVIEW_H#define SCREENPREVIEW_H#include<gpppacket.h>class ScreenPreview: public GPPPacket{ Q_PROPERTY(QString data READ getData) Q_PROPERTY(int index READ getIndex)private: QString data; int index=-1;public: ScreenPreview(int index,QString data); QString getData(){return data;} int getIndex(){return index;}};#endif // SCREENPREVIEW_H
QVariantMap MainWindow::toMap(QObject &obj){ QVariantMap map; for(int i=0; i<obj.metaObject()->propertyCount(); ++i) { if(obj.metaObject()->property(i).isStored(&obj)) { map.insert(obj.metaObject()->property(i).name(),obj.metaObject()->property(i).read(&obj)); } } return map;}
Q_PROPERTY(QString data READ getData STORED true)Q_PROPERTY(int index READ getIndex STORED true)
#ifndef SCREENPREVIEW_H#define SCREENPREVIEW_H#include<gpppacket.h>class ScreenPreview: public GPPPacket{ Q_PROPERTY(QString data READ getData) Q_PROPERTY(int index READ getIndex)private: QString data; int index=0;public: ScreenPreview(); QString getData(){return data;} int getIndex(){return index;}};#endif // SCREENPREVIEW_H
ScreenPreview preview;QMessageBox::information(this,"Информация",QJsonDocument::fromVariant(toMap(preview)).toJson());
{ "head":"", "objectName":""}