Results 1 to 7 of 7

Thread: QDataStream overloading << and >>

  1. #1
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QDataStream overloading << and >>

    Hi,

    I'm trying to override the QDataStream to insert a class into a stream eventually writing it to a file. The >> works however << seems to be broken.

    Kindly check and let me know what i'm missing

    main
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4.  
    5. WorkClass *w = new WorkClass;
    6. w->setId(1);
    7. w->setWorkname("Hello");
    8.  
    9. WorkItemClass *wc1 = new WorkItemClass;
    10. wc1->setId(11);
    11. wc1->setData("11");
    12.  
    13. WorkItemClass *wc2 = new WorkItemClass;
    14. wc2->setId(22);
    15. wc2->setData("22");
    16.  
    17. w->appendItem(wc1);
    18. w->appendItem(wc2);
    19.  
    20.  
    21. QFile file("test.txt");
    22. if(file.open(QIODevice::WriteOnly ))
    23. {
    24. QDataStream stream;
    25. stream.setDevice(&file);
    26. stream << w;
    27. file.close();
    28. }
    29.  
    30. WorkClass w2;
    31. if(file.open(QIODevice::ReadOnly))
    32. {
    33. QDataStream stream( &file );
    34. stream>>w2;
    35. file.close();
    36. }
    37.  
    38. return a.exec();
    39. }
    To copy to clipboard, switch view to plain text mode 

    workclass.h
    Qt Code:
    1. class WorkClass;
    2. class WorkItemClass;
    3.  
    4. class WorkClass : public QObject
    5. {
    6. Q_OBJECT
    7. public:
    8. explicit WorkClass(QObject *parent = nullptr);
    9.  
    10. QVariant getId() const;
    11. void setId(const QVariant &value);
    12.  
    13. QString getWorkname() const;
    14. void setWorkname(const QString &value);
    15.  
    16. QList<WorkItemClass*> getItemList() const;
    17. void appendItem( WorkItemClass *value);
    18.  
    19.  
    20. signals:
    21.  
    22. public slots:
    23.  
    24. protected:
    25. QString workname;
    26. QList<WorkItemClass*> itemList;
    27.  
    28. };
    29. QDataStream &operator>>(QDataStream &stream, WorkClass &work);
    30.  
    31. QDataStream &operator<<(QDataStream &stream,const WorkClass &work);
    32.  
    33. class WorkItemClass : public QObject
    34. {
    35. Q_OBJECT
    36. public:
    37. explicit WorkItemClass(QObject *parent = nullptr);
    38.  
    39. int getId() const;
    40. void setId(int value);
    41.  
    42. QString getData() const;
    43. void setData(const QString &value);
    44.  
    45. private:
    46. int id;
    47. QString data;
    48.  
    49. };
    To copy to clipboard, switch view to plain text mode 

    workclass.cpp
    Qt Code:
    1. QDataStream &operator>>(QDataStream &stream, WorkClass &work)
    2. {
    3. QVariant variant;
    4. QString string;
    5. int count;
    6. int id;
    7.  
    8. stream >> variant;
    9. work.setId(variant);
    10.  
    11. stream >> string;
    12. work.setWorkname(string);
    13.  
    14. stream >> count;
    15. for (int i=0; i <count; i++) {
    16. stream >> id;
    17. stream >> string;
    18. WorkItemClass *w = new WorkItemClass;
    19. w->setId(id);
    20. w->setData(string);
    21. work.appendItem(w);
    22. }
    23.  
    24. return stream;
    25. }
    26.  
    27. QDataStream &operator<<(QDataStream& stream, const WorkClass &work)
    28. {
    29. stream << work.getId();
    30. stream << work.getWorkname();
    31.  
    32.  
    33. stream << work.getItemList().size();
    34.  
    35. for(int i =0;i<work.getItemList().count();i++)
    36. {
    37. stream << work.getItemList().at(i)->getId();
    38. stream << work.getItemList().at(i)->getData();
    39. }
    40.  
    41. return stream;
    42. }
    43.  
    44.  
    45.  
    46. WorkClass::WorkClass(QObject *parent) : QObject(parent)
    47. {
    48.  
    49. }
    50.  
    51. QVariant WorkClass::getId() const
    52. {
    53. return id;
    54. }
    55.  
    56. void WorkClass::setId(const QVariant &value)
    57. {
    58. id = value;
    59. }
    60.  
    61. QString WorkClass::getWorkname() const
    62. {
    63. return workname;
    64. }
    65.  
    66. void WorkClass::setWorkname(const QString &value)
    67. {
    68. workname = value;
    69. }
    70.  
    71. QList<WorkItemClass*> WorkClass::getItemList() const
    72. {
    73. return itemList;
    74. }
    75.  
    76. void WorkClass::appendItem(WorkItemClass *value)
    77. {
    78. itemList.append(value);
    79. }
    80.  
    81. WorkItemClass::WorkItemClass(QObject *parent) : QObject (parent)
    82. {
    83.  
    84. }
    85.  
    86. int WorkItemClass::getId() const
    87. {
    88. return id;
    89. }
    90.  
    91. void WorkItemClass::setId(int value)
    92. {
    93. id = value;
    94. }
    95.  
    96. QString WorkItemClass::getData() const
    97. {
    98. return data;
    99. }
    100.  
    101. void WorkItemClass::setData(const QString &value)
    102. {
    103. data = value;
    104. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream overloading << and >>

    The operators are fine.

    The issue is that you are not writing your object into the stream but its pointer.

    Qt Code:
    1. QFile file("test.txt");
    2. if(file.open(QIODevice::WriteOnly ))
    3. {
    4. QDataStream stream;
    5. stream.setDevice(&file);
    6. stream << *w; // dereference the pointer and write the actual object
    7. file.close();
    8. }
    To copy to clipboard, switch view to plain text mode 

    As a recommendation: don't use QObject as a base class unless you need it. Here actual "value" type classes would likely better.
    Currently you are leaking all entries in itemList

    Cheers,
    _

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

    volcano (20th July 2019)

  4. #3
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream overloading << and >>

    Quote Originally Posted by anda_skoa View Post
    The operators are fine.

    The issue is that you are not writing your object into the stream but its pointer.

    Qt Code:
    1. QFile file("test.txt");
    2. if(file.open(QIODevice::WriteOnly ))
    3. {
    4. QDataStream stream;
    5. stream.setDevice(&file);
    6. stream << *w; // dereference the pointer and write the actual object
    7. file.close();
    8. }
    To copy to clipboard, switch view to plain text mode 



    Cheers,
    _

    Thanks for spotting that for me..

    As a recommendation: don't use QObject as a base class unless you need it. Here actual "value" type classes would likely better.
    Currently you are leaking all entries in itemList
    Will make sure to use QObject only if I need so.
    I guess parenting the entries int the itemList will stop the leak. Correct me if i'm mistaken.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream overloading << and >>

    Quote Originally Posted by volcano View Post
    I guess parenting the entries int the itemList will stop the leak. Correct me if i'm mistaken.
    Yes, or deleting them in the destructor, see qDeleteAll() for a convenient way to do that.

    Not using QObject as a base class would mean you could store the objects directly and not need pointers.

    Cheers,
    _

  6. #5
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream overloading << and >>

    Thanks for the prompt reply

    you could store the objects directly and not need pointers.
    Implies i could use

    WorkClass w;
    stream << w;

    instead of
    WorkClass *w = new WorkClass;
    stream << *w.

  7. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream overloading << and >>

    Yes and QList<WorkItemClass>

    Cheers,
    _

  8. #7
    Join Date
    Jan 2010
    Posts
    95
    Thanks
    14
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDataStream overloading << and >>

    Cool... Thank you
    volcano

Similar Threads

  1. Operator Overloading
    By naturalpsychic in forum Newbie
    Replies: 1
    Last Post: 19th July 2011, 06:19
  2. SOLVED: Operator overloading QDataStream
    By eekhoorn12 in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2010, 00:55
  3. QList Overloading operator==()
    By josepvr in forum Qt Programming
    Replies: 8
    Last Post: 28th January 2009, 16:28
  4. operator [] overloading
    By darksaga in forum General Programming
    Replies: 5
    Last Post: 8th April 2008, 16:27
  5. overloading ++ --
    By mickey in forum General Programming
    Replies: 13
    Last Post: 4th January 2008, 19:55

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.