Results 1 to 13 of 13

Thread: QTList Custom class Serlization

  1. #1
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Default 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.


    Qt Code:
    1. class CustomClass
    2. {
    3. QString name;
    4. QDataStream &operator<<(QDataStream &out, const CustomClass& myClass)
    5. {
    6. out<<myClass.name;
    7. return out;
    8. }
    9. QDataStream &operator>>(QDataStream& in,CustomClass& myClass)
    10. {
    11. in>>myClass.name;
    12. return in;
    13. }
    14. }
    15. Q_DECLARE_METATYPE(CustomClass*);
    16.  
    17. class CustomClass2
    18. {
    19. QList<CustomClass> list;
    20. QDataStream &operator<<(QDataStream &out, const CustomClass2& myClass)
    21. {
    22. out<<myClass.list;
    23. return out;
    24. }
    25. QDataStream &operator>>(QDataStream& in,CustomClass2& myClass)
    26. {
    27. in>>myClass.list;
    28. return in;
    29. }
    30. };
    To copy to clipboard, switch view to plain text mode 
    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

  2. #2
    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: QTList Custom class Serlization

    Define the operators as standalone functions and not methods of 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.


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

    saravanadel (20th January 2012)

  4. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTList Custom class Serlization

    Quote Originally Posted by saravanadel View Post
    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.

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

    saravanadel (20th January 2012)

  6. #4
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Default Re: QTList Custom class Serlization

    Quote Originally Posted by wysota View Post
    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 View Post
    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.
    Last edited by saravanadel; 20th January 2012 at 06:35.

  7. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default 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?
    Last edited by ChrisW67; 20th January 2012 at 07:01.

  8. The following user says thank you to ChrisW67 for this useful post:

    saravanadel (20th January 2012)

  9. #6
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Default Re: QTList Custom class Serlization

    Quote Originally Posted by ChrisW67 View Post
    What do your actual streaming operators look like?
    inline QDataStream& operator<<(QDataStream &out, const ContactModel& myClass)
    inline QDataStream& operator>>(QDataStream& in,ContactModel& myClass)

  10. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTList Custom class Serlization

    Those operators have nothing to do with your last example.

    This does not do what you think it does:
    Qt Code:
    1. QList<CustomClass*> list;
    2. QDataStream out(&file);
    3. out <<list; //works fine
    To copy to clipboard, switch view to plain text mode 
    Trace the execution and look at the actual data written to the file. Your streaming operator for CustomClass:
    Qt Code:
    1. QDataStream &operator<<(QDataStream &out, const CustomClass& myClass)
    To copy to clipboard, switch view to plain text mode 
    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.

  11. The following user says thank you to ChrisW67 for this useful post:

    saravanadel (20th January 2012)

  12. #8
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Default 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)

  13. #9
    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: QTList Custom class Serlization

    If an operator for streaming CustomClass objects looks like this:
    Qt Code:
    1. QDataStream &operator<<(QDataStream &out, const CustomClass& myClass)
    To copy to clipboard, switch view to plain text mode 
    Then what will an operator for streaming CustomClass* look like?
    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.


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

    saravanadel (25th January 2012)

  15. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTList Custom class Serlization

    Perhaps use the * operator to deference each pointer
    Qt Code:
    1. out << list.size();
    2. foreach(CustomClass* p, list)
    3. out << *p;
    To copy to clipboard, switch view to plain text mode 

  16. The following user says thank you to ChrisW67 for this useful post:

    saravanadel (25th January 2012)

  17. #11
    Join Date
    Jan 2012
    Posts
    9
    Thanks
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Symbian S60

    Default Re: QTList Custom class Serlization

    Quote Originally Posted by ChrisW67 View Post
    Perhaps use the * operator to deference each pointer
    Qt Code:
    1. out << list.size();
    2. foreach(CustomClass* p, list)
    3. out << *p;
    To copy to clipboard, switch view to plain text mode 
    This is how I am using now.

  18. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QTList Custom class Serlization

    Huh Nowhere in this thread have you done anything like that.
    Your code:
    Qt Code:
    1. QList<CustomClass*> list;
    2. QDataStream out(&file);
    3. out <<list; //works fine
    To copy to clipboard, switch view to plain text mode 

    My code:
    Qt Code:
    1. QList<CustomClass*> list;
    2. QDataStream out(&file);
    3. out << list.size();
    4. foreach(CustomClass* p, list)
    5. out << *p;
    To copy to clipboard, switch view to plain text mode 

    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.

  19. The following user says thank you to ChrisW67 for this useful post:

    saravanadel (25th January 2012)

  20. #13
    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: 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.
    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.


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

    saravanadel (25th January 2012)

Similar Threads

  1. Replies: 7
    Last Post: 18th August 2011, 15:43
  2. Using custom class as a key (QSet)
    By daujeroti in forum Qt Programming
    Replies: 4
    Last Post: 28th April 2011, 21:39
  3. qhash with custom class
    By dognzhe in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 07:30
  4. custom class for tslib
    By nrabara in forum Newbie
    Replies: 1
    Last Post: 28th April 2009, 14:15
  5. Custom Model Class
    By mattjgalloway in forum Qt Programming
    Replies: 12
    Last Post: 4th June 2007, 18:30

Tags for this Thread

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.