Hi everyone i have a foo class which is inherited from QObject and i want to serialize using QSettings. Code in foo.h
namespace GUI
{
class Foo : public QObject
{
Q_OBJECT
Q_PROPERTY(QString mem1 READ mem1 WRITE setMem1 NOTIFY mem1Changed)
Q_PROPERTY(QString mem2 READ mem2 WRITE setMem2 NOTIFY mem2Changed)
Q_PROPERTY(int mem3 READ mem3 WRITE setMem3 NOTIFY mem3Changed)
public:
explicit Foo(QObject *parent = 0);
virtual ~Foo();
....
friend QDataStream& operator>>(QDataStream& in, Foo* v);
friend QDataStream& operator<<(QDataStream& out, const Foo* v);
private:
QString mem1;
QString mem2;
int mem3;
};
}
Q_DECLARE_METATYPE(GUI::Foo*)
Code in Foo.cpp
QDataStream& operator<<(QDataStream& out, const Foo* v) {
out << v->mem1 << v->mem2<< v->mem3;
return out;
}
QDataStream& operator>>(QDataStream& in, Foo& v) {
in >>v->mem1;
in >>v->mem2;
in >>v->mem3
return in;
}
Code in main.cpp
qRegisterMetaTypeStreamOperators<GUI::Foo*>("GUI::Foo*");
GUI::Foo c1 ;
QSettings saver;
saver.setValue("test1",QVariant::fromValue(&c1));
QSettings loader;
QVariant value = loader.value("test1");
GUI::Foo* t = value.value<GUI::Foo*>();
Now this is compiling fine but when i debug it, i get Exception Triggered read access violation
QDataStream& operator>>(QDataStream& in, Foo& v) {
in >>v->mem1; <- here i am getting read access violation and v is showing to be null
in >>v->mem2;
in >>v->mem3
return in;
}
One thing important to note is that if i change my friend function to
QDataStream& operator>>(QDataStream& in, Foo* v) {
QString var1;
QString var2;
int var3;
in >> var1;
in >> var2;
in >> var3;
return in;
}
i do get the correct values in var1,var2 and var3.
Can someone please tell me what i am doing wrong and how can i rectify it Thanks in advance