QTList Custom class Serlization
Hi,
I have a problem in qt list serialization.
I am getting error no match for operator '>>' in s '>>' t (from qdatastream.h). I also tried declaring meta but not use.
Code:
class CustomClass
{
{
out<<myClass.name;
return out;
}
QDataStream &operator>>
(QDataStream
& in,CustomClass
& myClass
) {
in>>myClass.name;
return in;
}
}
Q_DECLARE_METATYPE(CustomClass*);
class CustomClass2
{
QList<CustomClass> list;
{
out<<myClass.list;
return out;
}
QDataStream &operator>>
(QDataStream
& in,CustomClass2
& myClass
) {
in>>myClass.list;
return in;
}
};
Without the meta tag declaration 'Q_DECLARE_METATYPE(CustomClass*);'. it says no match for operator '>>' and '<<' myClass.list
Please help.
Development in,
Qt SDK for Nokia
Re: QTList Custom class Serlization
Define the operators as standalone functions and not methods of your class.
Re: QTList Custom class Serlization
Quote:
Originally Posted by
saravanadel
Without the meta tag declaration 'Q_DECLARE_METATYPE(CustomClass*);'. it says no match for operator '>>' and '<<' myClass.list
With the macro in place you will receive an error because CustomClass declaration is missing a terminating semi-colon.
After doing as wysota suggests you may also need to make the free operator functions friends of the class.
Re: QTList Custom class Serlization
Quote:
Originally Posted by
wysota
Define the operators as standalone functions and not methods of your class.
Still the same.
no match for 'operator >>' in 'in >> myClass.list'
I missed the semi-colon in the post.
Added after 41 minutes:
Quote:
Originally Posted by
wysota
Define the operators as standalone functions and not methods of your class.
It worked when I declared them inline in h.
Secondly I cannot serialize the objects to the datastream.
QList<CustomClass*> list;
QDataStream out(&file);
out <<list; //works fine
QList<CustomClass*> ob;
QDataStream in(&file);
in >>ob; //no match for operator' >> in s >> t'
I more thing, I am inheriting CustomClass from QObject.
Re: QTList Custom class Serlization
Congratulations. Would you like us to guess what you have actually done? Clearly it wasn't what wysota and I suggested because I know that works.
Edit... since you added to your prior post:
In your header you should have a prototype for the operator>> and operator<< free functions.
In your cpp you should have the implementation. Just like you do for the classes.
You are probably streaming a series of pointer values to a store, not the content of the pointed-to objects. This does not bode well for recovering the objects. What do your actual streaming operators look like?
Re: QTList Custom class Serlization
Quote:
Originally Posted by
ChrisW67
What do your actual streaming operators look like?
inline QDataStream& operator<<(QDataStream &out, const ContactModel& myClass)
inline QDataStream& operator>>(QDataStream& in,ContactModel& myClass)
Re: QTList Custom class Serlization
Those operators have nothing to do with your last example.
This does not do what you think it does:
Code:
QList<CustomClass*> list;
out <<list; //works fine
Trace the execution and look at the actual data written to the file. Your streaming operator for CustomClass:
is not being used. The pointers are being streamed as booleans.
When you have worked out why then you can tackle the second part with some understanding.
Re: QTList Custom class Serlization
QList<CustomClass*> list;
Is there any way to write the values of the qlist rather than streaming the pointers.
If I declare 'QList<CustomClass>' it says Object is private, since the copying is not allowed. (But I can do this with inheriting QObject which I dont want)
Re: QTList Custom class Serlization
If an operator for streaming CustomClass objects looks like this:
Then what will an operator for streaming CustomClass* look like?
Re: QTList Custom class Serlization
Perhaps use the * operator to deference each pointer
Code:
out << list.size();
foreach(CustomClass* p, list)
out << *p;
Re: QTList Custom class Serlization
Quote:
Originally Posted by
ChrisW67
Perhaps use the * operator to deference each pointer
Code:
out << list.size();
foreach(CustomClass* p, list)
out << *p;
This is how I am using now.
Re: QTList Custom class Serlization
Huh :confused: Nowhere in this thread have you done anything like that.
Your code:
Code:
QList<CustomClass*> list;
out <<list; //works fine
My code:
Code:
QList<CustomClass*> list;
out << list.size();
foreach(CustomClass* p, list)
out << *p;
Wysota's prompting would give you something that would make your code work.
My code gives you another way to do it using the operator<<() for CustomClass that you already have.
Re: QTList Custom class Serlization
Effectively the operator would do more or less the same :)
Just a word of warning -- if you use the solution provided by Chris, you have to serialize size of the list as well otherwise while deserializing you won't know how many items to read.