So what is the problem? The StackOverflow answer tells you exactly what you need to do: implement the >> and << operators for your classes:
QDataStream &operator<<(QDataStream &out, const Answer & answer);
QDataStream &operator>>(QDataStream &in, Answer & answer);
QDataStream &operator<<(QDataStream &out, const Card & card);
QDataStream &operator>>(QDataStream &in, Card & card);
To copy to clipboard, switch view to plain text mode
The only tricky part is how to serialize the QVector<Answer> members. The usual way is to first serialize the count (number of members in the vector), then serialize each member.
QDataStream & operator<<
( QDataSream
& out,
const Card
& card
) {
int nAnswers = card.getAnswers.size();
out << nAnswers;
foreach( answer, card.getAnswers() )
out << answer; // assumes you have implemented operator<< for the Answer class
// do other Card data elements
return out;
}
QDataStream & operator<<( QDataSream & out, const Card & card )
{
int nAnswers = card.getAnswers.size();
out << nAnswers;
foreach( answer, card.getAnswers() )
out << answer; // assumes you have implemented operator<< for the Answer class
// do other Card data elements
return out;
}
To copy to clipboard, switch view to plain text mode
You might find it convenient to add set...() methods to your classes (Card::setAnswers( const QVector< Answer > & answers ), etc.) because the way you have defined Card and Answer, the only way you can set these fields is in the constructor - there is no way to set them independently, for a Card or Answer instance that already exists.
You should also use references to pass or return by reference instead of by value:
const QVector< Answer > & Card::getAnswers() const;
const QVector< Answer > & Card::getAnswers() const;
To copy to clipboard, switch view to plain text mode
The way you have implemented it, this methods makes a complete copy of the QVector before returning it. Using a reference means that no copying is done.
Bookmarks