i use Reading/writing QObjects is it true? i serialize a class with it but when deserialize it isn't the original class!
我用读/写QObjects是真的吗?我用它序列化一个类,但是当反序列化它不是原始类!
what can i do?
我能做什么?
this is my base class header:
这是我的基类头:
class Base : public QObject
{
Q_OBJECT
public:
explicit Base(QObject *parent = 0);
};
QDataStream &operator<<(QDataStream &ds, const Base &obj);
QDataStream &operator>>(QDataStream &ds, Base &obj) ;
and .cpp is:
和.cpp是:
Base::Base(QObject *parent) :
QObject(parent)
{
}
QDataStream &operator<<(QDataStream &ds, const Base &obj) {
for(int i=0; i<obj.metaObject()->propertyCount(); ++i) {
if(obj.metaObject()->property(i).isStored(&obj)) {
ds << obj.metaObject()->property(i).read(&obj);
}
}
return ds;
}
QDataStream &operator>>(QDataStream &ds, Base &obj) {
QVariant var;
for(int i=0; i<obj.metaObject()->propertyCount(); ++i) {
if(obj.metaObject()->property(i).isStored(&obj)) {
ds >> var;
obj.metaObject()->property(i).write(&obj, var);
}
}
return ds;
}
and i have a student class that inherit from base :
我有一个继承基地的学生班:
class student : public Base
{
public:
student();
int id;
QString Name;
};
and it is my main :
这是我的主要内容:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
student G;
student G2;
G.id=30;
G.Name="erfan";
qDebug()<<G.id<<G.Name;
QFile file("file.dat");
file.open(QIODevice::WriteOnly);
QDataStream out(&file); // we will serialize the data into the file
out <<G;
qDebug()<<G2.id<<G2.Name;
file.close();
file.open(QIODevice::ReadOnly);
out>>G2;
qDebug()<<G2.id<<G2.Name;
return a.exec();
}
and it is my output:
这是我的输出:
30 "erfan"
1498018562 ""
1498018562 ""
1 个解决方案
#1
13
You must make id
and Name
as Q_PROPERTY to handle it with metaObject property system:
您必须将id和Name设置为Q_PROPERTY才能使用metaObject属性系统处理它:
class student : public Base
{
Q_OBJECT // Q_OBJECT macro will take care of generating proper metaObject for your class
Q_PROPERTY(int id READ getId WRITE setId)
Q_PROPERTY(QString Name READ getName WRITE setName)
public:
student();
int getId() const { return id; }
void setId(int newId) { id = newId; }
QString getName() const { return Name; }
void setName(const QString &newName) { Name = newName; }
private:
int id;
QString Name;
};
Now properties should be handled in proper way.
现在属性应该以适当的方式处理。
See http://doc.qt.io/qt-5/properties.html for detailed information.
有关详细信息,请参见http://doc.qt.io/qt-5/properties.html。
#1
13
You must make id
and Name
as Q_PROPERTY to handle it with metaObject property system:
您必须将id和Name设置为Q_PROPERTY才能使用metaObject属性系统处理它:
class student : public Base
{
Q_OBJECT // Q_OBJECT macro will take care of generating proper metaObject for your class
Q_PROPERTY(int id READ getId WRITE setId)
Q_PROPERTY(QString Name READ getName WRITE setName)
public:
student();
int getId() const { return id; }
void setId(int newId) { id = newId; }
QString getName() const { return Name; }
void setName(const QString &newName) { Name = newName; }
private:
int id;
QString Name;
};
Now properties should be handled in proper way.
现在属性应该以适当的方式处理。
See http://doc.qt.io/qt-5/properties.html for detailed information.
有关详细信息,请参见http://doc.qt.io/qt-5/properties.html。