Hi,
I have a problem with class serialization.
I have two classes (one derived from the other)
I provided operator << for the two classes and all works fine if I use the class separately.
I have a problem when I try to use them in a List containing instances of the two classes: it is used always the << operator of the parent class.
Here's an example.
Two classes (Test2 just adds a QString property to Test):
{
Q_OBJECT
public:
explicit Test
(QObject *parent
= 0);
Test(const Test& test);
int Field_3;
};
class Test2 : public Test
{
Q_OBJECT
public:
explicit Test2
(QObject *parent
= 0);
Test2(const Test2& test2);
};
{
}
Test::Test(const Test& test)
{
Field_1 = test.Field_1;
Field_2 = test.Field_2;
Field_3 = test.Field_3;
}
Test(parent)
{
}
Test2::Test2(const Test2& test2) :
Test(test2)
{
Field_4 = test2.Field_4;
}
Q_DECLARE_METATYPE(Test)
{
out << test.Field_1 << test.Field_2 << test.Field_3;
return out;
}
{
in >> test.Field_1 >> test.Field_2 >> test.Field_3;
return in;
}
Q_DECLARE_METATYPE(Test2)
{
out << test.Field_1 << test.Field_2 << test.Field_3 << test.Field_4;
return out;
}
{
in >> test.Field_1 >> test.Field_2 >> test.Field_3 >> test.Field_4;
return in;
}
class Test : public QObject
{
Q_OBJECT
public:
explicit Test(QObject *parent = 0);
Test(const Test& test);
QString Field_1;
QString Field_2;
int Field_3;
};
class Test2 : public Test
{
Q_OBJECT
public:
explicit Test2(QObject *parent = 0);
Test2(const Test2& test2);
QString Field_4;
};
Test::Test(QObject *parent) :
QObject(parent)
{
}
Test::Test(const Test& test)
{
Field_1 = test.Field_1;
Field_2 = test.Field_2;
Field_3 = test.Field_3;
}
Test2::Test2(QObject *parent) :
Test(parent)
{
}
Test2::Test2(const Test2& test2) :
Test(test2)
{
Field_4 = test2.Field_4;
}
Q_DECLARE_METATYPE(Test)
QDataStream &operator<<(QDataStream &out, const Test &test)
{
out << test.Field_1 << test.Field_2 << test.Field_3;
return out;
}
QDataStream &operator>>(QDataStream &in, Test &test)
{
in >> test.Field_1 >> test.Field_2 >> test.Field_3;
return in;
}
Q_DECLARE_METATYPE(Test2)
QDataStream &operator<<(QDataStream &out, const Test2 &test)
{
out << test.Field_1 << test.Field_2 << test.Field_3 << test.Field_4;
return out;
}
QDataStream &operator>>(QDataStream &in, Test2 &test)
{
in >> test.Field_1 >> test.Field_2 >> test.Field_3 >> test.Field_4;
return in;
}
To copy to clipboard, switch view to plain text mode
If I put in a List instances of the two class in the ini file I always get just a Test serialization:
QList<Test*> list;
Test* t = new Test(this);
t->Field_1 = "Field 1";
t->Field_2 = "Field 2";
t->Field_3 = 10;
list.append(t);
Test2* t2 = new Test2(this);
t2->Field_1 = "Field 1";
t2->Field_2 = "Field 2";
t2->Field_3 = 10;
t2->Field_4 = "Field 4";
list.append(t2);
settings.beginWriteArray("tests");
int index = 0;
foreach (Test* o, list){
settings.setArrayIndex(index);
settings.setValue("test", qVariantFromValue(*o));
index++;
}
settings.endArray();
settings.sync();
QSettings settings;
QList<Test*> list;
Test* t = new Test(this);
t->Field_1 = "Field 1";
t->Field_2 = "Field 2";
t->Field_3 = 10;
list.append(t);
Test2* t2 = new Test2(this);
t2->Field_1 = "Field 1";
t2->Field_2 = "Field 2";
t2->Field_3 = 10;
t2->Field_4 = "Field 4";
list.append(t2);
settings.beginWriteArray("tests");
int index = 0;
foreach (Test* o, list){
settings.setArrayIndex(index);
settings.setValue("test", qVariantFromValue(*o));
index++;
}
settings.endArray();
settings.sync();
To copy to clipboard, switch view to plain text mode
Is it normal? How can I correctly serialize that Test2 instance?
Many thanks
Bookmarks