Results 1 to 2 of 2

Thread: Serializing my Qt class with QDataStream

  1. #1
    Join Date
    Oct 2019
    Qt products
    MacOS X Unix/X11 Windows Android

    Question Serializing my Qt class with QDataStream

    Hello, i am working on my Quizproject but recently i stumbled upon a problem.I need to serialize my Qt class.I found this example on the internet, it's the first answer.I want to implement it on a similar way on my class here's my class:
    Qt Code:
    1. class Answer{
    2. private:
    3. qint8 id;
    4. QString answer;
    6. public:
    7. Answer(QString nAnswer, qint8 nId);
    8. QString getAnswer();
    9. qint8 getId();
    11. };
    13. class Card
    14. {
    15. private:
    16. QString question;
    17. QVector<Answer> answers;
    18. qint8 solutionId;
    20. public:
    21. Card(QString nQuestion, qint8 nSolutionId, QVector<Answer> nAnswers);
    22. QString getQuestion();
    23. QVector<Answer> getAnswers();
    24. qint8 getSolutionId();
    26. };
    28. Answer::Answer(QString nAnswer, qint8 nId)
    29. {
    30. answer = nAnswer;
    31. id = nId;
    32. }
    35. Card::Card(QString nQuestion, qint8 nSolutionId, QVector<Answer> nAnswers)
    36. {
    37. question = nQuestion;
    38. solutionId = nSolutionId;
    39. answers = nAnswers;
    40. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Alameda, CA, USA
    Thanked 662 Times in 652 Posts
    Qt products
    Windows Android

    Default Re: Serializing my Qt class with QDataStream

    So what is the problem? The StackOverflow answer tells you exactly what you need to do: implement the >> and << operators for your classes:

    Qt Code:
    1. QDataStream &operator<<(QDataStream &out, const Answer & answer);
    2. QDataStream &operator>>(QDataStream &in, Answer & answer);
    4. QDataStream &operator<<(QDataStream &out, const Card & card);
    5. 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.

    Qt Code:
    1. QDataStream & operator<<( QDataSream & out, const Card & card )
    2. {
    3. int nAnswers = card.getAnswers.size();
    4. out << nAnswers;
    5. foreach( answer, card.getAnswers() )
    6. out << answer; // assumes you have implemented operator<< for the Answer class
    8. // do other Card data elements
    10. return out;
    11. }
    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:
    Qt Code:
    1. 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.
    Last edited by d_stranz; 5th November 2019 at 23:57.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 4
    Last Post: 9th January 2016, 10:55
  2. Replies: 2
    Last Post: 12th December 2013, 15:25
  3. Serializing a Qobject
    By erfan in forum Qt Programming
    Replies: 1
    Last Post: 12th December 2012, 18:00
  4. QDataStream class/struct & stream operators
    By darksaga in forum Qt Programming
    Replies: 1
    Last Post: 1st August 2008, 20:40
  5. Serializing
    By xyzt in forum Qt Programming
    Replies: 1
    Last Post: 23rd March 2008, 09:51

Tags for this Thread


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.