Results 1 to 3 of 3

Thread: Serialize nested user defined class in Q_PROPERTY

  1. #1
    Join Date
    Feb 2017
    Posts
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Question Serialize nested user defined class in Q_PROPERTY

    I faced with following problem: I can't serialize user defined object from Q_PROPERTY
    I try to serialize RegistersSettings class to QDataStream. The idea is to be able to serialize it to text file (using << operator) and later be able to read it (using >> operator). It should verify that fields that was readed from file are still valid. So I inspect property for that.
    The issue is that Q_PROPERTY(QList<RegisterGroupSettings> groups MEMBER groups) is not work as expected.
    It looks like it's possible to create such functionality, but it looks that it's not so easy.
    Could anyone help with the common way how to serialize user defined class from Q_PROPERTY?

    The code is simplifyed to be more readable, but the main idea is in place.

    Qt Code:
    1. class RegisterGroupSettings:SettingsItem<RegisterGroupSettings>
    2. {
    3. private:
    4. Q_GADGET
    5. Q_PROPERTY(QString name MEMBER name)
    6. Q_PROPERTY(int interval MEMBER interval)
    7.  
    8. public:
    9. QString name;
    10. int interval;
    11. };
    12. Q_DECLARE_METATYPE(RegisterGroupSettings)
    13.  
    14. class RegistersSettings:SettingsItem<RegistersSettings>
    15. {
    16. private:
    17. Q_GADGET
    18. Q_PROPERTY(QList<RegisterGroupSettings> groups MEMBER groups)
    19. Q_PROPERTY(int code MEMBER code)
    20.  
    21. public:
    22. QList<RegisterGroupSettings> groups;
    23. int code;
    24. };
    25. Q_DECLARE_METATYPE(RegistersSettings)
    To copy to clipboard, switch view to plain text mode 

    SettingsItem is a helper fo unification

    Qt Code:
    1. template <typename T> class SettingsItem
    2. {
    3. public:
    4. friend QDataStream & operator << (QDataStream &arch, const T & object)
    5. {
    6. const QMetaObject &mo = object.staticMetaObject;
    7. int cnt = mo.propertyCount();
    8. QString prop_name;
    9. QVariant prop_value;
    10. arch << cnt;
    11. while (cnt>0)
    12. {
    13. prop_name = mo.property(cnt-1).name();
    14. prop_value = mo.property(cnt-1).readOnGadget(&object);
    15. arch << prop_name;
    16. arch << prop_value;
    17. cnt--;
    18. }
    19. return arch;
    20. }
    21.  
    22. friend QDataStream & operator >> (QDataStream &arch, T & object)
    23. {
    24. const QMetaObject &mo = object.staticMetaObject;
    25. int cnt=0;
    26. QString prop_name;
    27. QVariant prop_value;
    28. int prop_index;
    29. arch >> cnt;
    30. while (cnt>0)
    31. {
    32. arch >> prop_name;
    33. arch >> prop_value;
    34. prop_index = mo.indexOfProperty(prop_name.toStdString().c_str());
    35. if (prop_index > -1)
    36. {
    37. mo.property(prop_index).writeOnGadget(&object, prop_value);
    38. }
    39. cnt--;
    40. }
    41. return arch;
    42. }
    43.  
    44. friend bool operator == (const T &first, const T &second)
    45. {
    46. const QMetaObject &mo = first.staticMetaObject;
    47. int cnt = mo.propertyCount();
    48. QString prop_name;
    49. QVariant oProp_value;
    50. QVariant dProp_value;
    51. while (cnt>0)
    52. {
    53. prop_name = mo.property(cnt-1).name();
    54. oProp_value = mo.property(cnt-1).readOnGadget(&first);
    55. dProp_value = mo.property(cnt-1).readOnGadget(&second);
    56. if (oProp_value == dProp_value)
    57. {
    58. cnt--;
    59. continue;
    60. }
    61. return false;
    62. }
    63.  
    64. return true;
    65. }
    66.  
    67. friend bool operator != (const T &first, const T &second)
    68. {
    69. return !( first == second );
    70. }
    71. };
    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: Serialize nested user defined class in Q_PROPERTY

    Can you give an indication of what you current problem is?

    Does it not compile?
    Are the operators not called?
    Do they fail?

    Cheers,
    _

  3. #3
    Join Date
    Feb 2017
    Posts
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: Serialize nested user defined class in Q_PROPERTY

    It doesn't work.
    But i have found the solution:

    The solution is to extend template with constructor

    Qt Code:
    1. SettingsItem()
    2. {
    3. qRegisterMetaType<T>();
    4. qRegisterMetaTypeStreamOperators<T>(T::staticMetaObject.className());
    5. }
    To copy to clipboard, switch view to plain text mode 
    and register nested type in class constructor

    Qt Code:
    1. RegistersSettings()
    2. {
    3. qRegisterMetaTypeStreamOperators<QList<RegisterGroupSettings>>("QList<RegisterGroupSettings>");
    4. }
    To copy to clipboard, switch view to plain text mode 

  4. The following user says thank you to EagleNN for this useful post:

    anda_skoa (3rd February 2017)

Similar Threads

  1. Replies: 6
    Last Post: 3rd December 2012, 07:26
  2. Serialize class objects to XML file and back
    By StarShaper in forum Qt Programming
    Replies: 2
    Last Post: 25th June 2012, 08:27
  3. Q_PROPERTY and deeply nested custom types
    By Sammy D'Souza in forum Newbie
    Replies: 1
    Last Post: 15th May 2010, 11:26
  4. User Defined Signal?
    By rajeshs in forum Newbie
    Replies: 2
    Last Post: 18th December 2007, 11:42
  5. Replies: 4
    Last Post: 26th June 2007, 19:19

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.