Results 1 to 7 of 7

Thread: Class serialization

  1. #1
    Join Date
    Apr 2010
    Location
    Rzeszów \ Poland
    Posts
    26
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Class serialization

    Hello.
    I have class like this:
    Qt Code:
    1. class pytanie
    2. {
    3. public:
    4. int ID;
    5. QString pytanie;
    6. QString odp1;
    7. QString odp2;
    8. QString odp3;
    9. QString odp4;
    10. };
    To copy to clipboard, switch view to plain text mode 

    and I creating a dynamic array by QVector like this:
    Qt Code:
    1. QVector<pytanie> pyt;
    To copy to clipboard, switch view to plain text mode 

    Now, I want to save (and serialize) objects of this classes to file. I'm using QDataStream. I'm trying few time, buy I still can't do it.

    I trying for example:
    Qt Code:
    1. QDataStream out (&plik);
    2. out << pyt[x];
    To copy to clipboard, switch view to plain text mode 
    but it doesnt work..

    Anyone can help me?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Class serialization

    you have to provide the "QDataStream << operator" for your class. Search the forum, about a year ago there where similar questions (with answers).

  3. #3
    Join Date
    Apr 2010
    Location
    Rzeszów \ Poland
    Posts
    26
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Class serialization

    I know, I tried to do it, but I don't know how to do it exactly.
    For example:
    Qt Code:
    1. QDataStream out(&file);
    2. out.operator <<(QVector<pytanie> & pyt[x]);
    To copy to clipboard, switch view to plain text mode 

    But this doesn't work...

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Class serialization

    You need to define an operator, not try using something which already exists (ignoring the fact that this code is not even syntactically correct).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    matulik (12th December 2010)

  6. #5
    Join Date
    Apr 2010
    Location
    Rzeszów \ Poland
    Posts
    26
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Class serialization

    OK. To understand this, I was trying to save QVector<int> object.
    So I created two function to save and load:
    Qt Code:
    1. void MainWindow::save()
    2. {
    3. QFile file("path....");
    4. if(!plik.open(QIODevice::ReadWrite))
    5. return;
    6. else
    7. {
    8. QDataStream out(&file);
    9. QDataStream & operator<<(QDataStream & out, const QVector<int> & asd);
    10. out << asd[0];
    11.  
    12. qDebug()<<"ok";
    13. plik.close();
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void MainWindow::load()
    2. {
    3. QFile plik("path....");
    4. if(!plik.open(QIODevice::ReadWrite))
    5. return;
    6. else
    7. {
    8. QDataStream in(&file);
    9. QDataStream & operator>>(QDataStream & in, QVector<int> & asd);
    10.  
    11. asd.insert(0,1);
    12. in >> asd [0];
    13.  
    14. qDebug()<<"ok!";
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 
    and this working great (now for only one item in array).
    But now I have two question about that:
    1. Can't I save all of asd objects (without loop of asd[x])?
    2. What I can save object of QVector<myOwnClass>?

    I tried:
    Qt Code:
    1. QDataStream out(&plik);
    2. QDataStream & operator<<(QDataStream & out, const QVector<pytania> & pyt);
    3. out << pyt[0];
    To copy to clipboard, switch view to plain text mode 

    but this doesn't works..

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Class serialization

    Quote Originally Posted by matulik View Post
    Qt Code:
    1. QDataStream out(&file);
    2. QDataStream & operator<<(QDataStream & out, const QVector<int> & asd);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. QDataStream in(&file);
    2. QDataStream & operator>>(QDataStream & in, QVector<int> & asd);
    To copy to clipboard, switch view to plain text mode 
    What is this supposed to do? Do you understand what you have written here? Also do you understand what we mean when we say you should implement QDataStream streaming operators for your class?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Class serialization

    Also do you understand what we mean when we say you should implement QDataStream streaming operators for your class?
    Obviously not. This is more of a retorical question, no ?

    @ matulik : you can't just create a class and expect << to work on it. the << operator is sort of like a normal method of your class (not really, but I'm just making a point). You must define this operator for your class, just like you define other methods of your class.

    You need to find yourself a book or tutorial on C++ if you want to understand how to do this. This is a pure C++ question, unrelated to Qt.

    Best regards,
    Marc

Similar Threads

  1. Serialization of QTextEdit
    By naghekyan in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2010, 06:02
  2. QDataStream and serialization
    By pdoria in forum Qt Programming
    Replies: 5
    Last Post: 11th November 2009, 09:42
  3. XML Serialization of Qt Objects
    By sasi in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2009, 19:25
  4. Need help with serialization of QMaps
    By fg_psycho in forum Qt Programming
    Replies: 1
    Last Post: 5th May 2009, 17:16
  5. Serialization
    By donmorr in forum Qt Programming
    Replies: 4
    Last Post: 16th November 2006, 13:51

Bookmarks

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.