Results 1 to 2 of 2

Thread: QSettings custom class serialization problem

  1. #1
    Join Date
    Jul 2011
    Location
    Italy
    Posts
    24
    Thanks
    2
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default QSettings custom class serialization problem

    Hi,

    I have a problem with class serialization.
    I have two classes (one derived from the other)
    I provided operator << for the two classes and all works fine if I use the class separately.
    I have a problem when I try to use them in a List containing instances of the two classes: it is used always the << operator of the parent class.

    Here's an example.
    Two classes (Test2 just adds a QString property to Test):
    Qt Code:
    1. class Test : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit Test(QObject *parent = 0);
    6. Test(const Test& test);
    7.  
    8. QString Field_1;
    9. QString Field_2;
    10. int Field_3;
    11. };
    12.  
    13. class Test2 : public Test
    14. {
    15. Q_OBJECT
    16. public:
    17. explicit Test2(QObject *parent = 0);
    18. Test2(const Test2& test2);
    19.  
    20. QString Field_4;
    21. };
    22.  
    23. Test::Test(QObject *parent) :
    24. QObject(parent)
    25. {
    26. }
    27.  
    28. Test::Test(const Test& test)
    29. {
    30. Field_1 = test.Field_1;
    31. Field_2 = test.Field_2;
    32. Field_3 = test.Field_3;
    33. }
    34.  
    35. Test2::Test2(QObject *parent) :
    36. Test(parent)
    37. {
    38. }
    39.  
    40. Test2::Test2(const Test2& test2) :
    41. Test(test2)
    42. {
    43. Field_4 = test2.Field_4;
    44. }
    45.  
    46. Q_DECLARE_METATYPE(Test)
    47. QDataStream &operator<<(QDataStream &out, const Test &test)
    48. {
    49. out << test.Field_1 << test.Field_2 << test.Field_3;
    50. return out;
    51. }
    52.  
    53. QDataStream &operator>>(QDataStream &in, Test &test)
    54. {
    55. in >> test.Field_1 >> test.Field_2 >> test.Field_3;
    56. return in;
    57. }
    58.  
    59. Q_DECLARE_METATYPE(Test2)
    60. QDataStream &operator<<(QDataStream &out, const Test2 &test)
    61. {
    62. out << test.Field_1 << test.Field_2 << test.Field_3 << test.Field_4;
    63. return out;
    64. }
    65.  
    66. QDataStream &operator>>(QDataStream &in, Test2 &test)
    67. {
    68. in >> test.Field_1 >> test.Field_2 >> test.Field_3 >> test.Field_4;
    69. return in;
    70. }
    To copy to clipboard, switch view to plain text mode 

    If I put in a List instances of the two class in the ini file I always get just a Test serialization:
    Qt Code:
    1. QSettings settings;
    2.  
    3. QList<Test*> list;
    4.  
    5. Test* t = new Test(this);
    6. t->Field_1 = "Field 1";
    7. t->Field_2 = "Field 2";
    8. t->Field_3 = 10;
    9. list.append(t);
    10.  
    11. Test2* t2 = new Test2(this);
    12. t2->Field_1 = "Field 1";
    13. t2->Field_2 = "Field 2";
    14. t2->Field_3 = 10;
    15. t2->Field_4 = "Field 4";
    16. list.append(t2);
    17.  
    18. settings.beginWriteArray("tests");
    19. int index = 0;
    20. foreach (Test* o, list){
    21. settings.setArrayIndex(index);
    22. settings.setValue("test", qVariantFromValue(*o));
    23. index++;
    24. }
    25. settings.endArray();
    26. settings.sync();
    To copy to clipboard, switch view to plain text mode 

    Is it normal? How can I correctly serialize that Test2 instance?

    Many thanks
    Last edited by sakya; 19th December 2011 at 11:47.

  2. #2
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSettings custom class serialization problem

    Quote Originally Posted by sakya View Post
    Is it normal?
    Yes.

    Quote Originally Posted by sakya View Post
    How can I correctly serialize that Test2 instance?
    In this case, it would be fairly easy to tweak the code to serialize properly but a lot more annoying to deserialize. Basically you'd have to move the serialization code to a virtual method of Test and the deserialization to some kind of factory. To distinguish between serialization of different types you'd then need to prefix the serialized data with a id (unique to each supported class).
    Current Qt projects : QCodeEdit, RotiDeCode

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

    sakya (19th December 2011)

Similar Threads

  1. Replies: 0
    Last Post: 10th December 2011, 12:17
  2. Replies: 7
    Last Post: 18th August 2011, 15:43
  3. Class serialization
    By matulik in forum Qt Programming
    Replies: 6
    Last Post: 13th December 2010, 13:17
  4. Replies: 6
    Last Post: 7th December 2010, 13:32
  5. Replies: 2
    Last Post: 12th May 2010, 14:32

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.