Hi,

If you need to convert a lots of you own non-QObject based class to XML/YAML/JSON etc. You may be interested with my tiny project - QtMapStream.

Source code:
https://gitorious.org/qtmapstream/qtmapstream#more

QVariantMap is a synonym of QMap<QString, QVariant>. It could represent any complex objects. Convert
a QVariantMap instance to data serialization formats like XML , JSON and YAML is much easier than
a complex objects in C++. However, convert a C++ class to QVariantMap is not strict forward. User
have to implements their own convertion function.

The QtMapStream provides an interface and utility to convert user's own C++ class , which is not based
on QObject , to QVariantMap and vice versa.

Example class:

Qt Code:
  1. class Book {
  2. public:
  3. QString title;
  4. int pages;
  5. qreal rating;
  6. }
To copy to clipboard, switch view to plain text mode 


You should add the QT_MAP_STREAMABLE / QT_MAP_STREAMABLE_ITEM macro to the class declaration:


Qt Code:
  1. class Book {
  2. public:
  3. QString title;
  4. int pages;
  5. qreal rating;
  6.  
  7. QT_MAP_STREAMABLE(
  8. QT_MAP_STREAMABLE_ITEM(QString,title)
  9. QT_MAP_STREAMABLE_ITEM(int,pages)
  10. QT_MAP_STREAMABLE_ITEM(qreal,rating)
  11. )
  12. };
To copy to clipboard, switch view to plain text mode 


The macro will add <<,>> operators overloading with taking a QtMapStream to the class.

Convert to QVariantMap


Qt Code:
  1. Book book;
  2. book.title = "Qt Developer Guides";
  3. book.pages = 180;
  4. book.rating = 10.0;
  5.  
  6. QtMapStream stream;
  7. book >> stream;
  8.  
  9. QVariantMap source = stream.source(); // It will return the QVariantMap writen by the book instance.
  10.  
  11. qDebug() << source; // QMap(("pages", QVariant(int, 180) ) ( "rating" , QVariant(double, 10) ) ( "title" , QVariant(QString, "Qt Developer Guides") ) )
To copy to clipboard, switch view to plain text mode 


Convert from QVariantMap

Qt Code:
  1. Book book;
  2. QVariantMap map;
  3. map["title"] = "Qt Developer Guides";
  4. map["pages"] = 180;
  5. map["rating"] = 10.0;
  6.  
  7. QtMapStream stream(map);
  8. book << stream; // The book instance will be filled with the information in QVariantmap
  9.  
  10. QVariantMap remaining = stream.source(); // After run the << stream operators, written field will be removed from the source. Call source() should return an empty map.
To copy to clipboard, switch view to plain text mode 


QtMapStream also support QList type. However, QList type can not be converted to QVariant directly. You should declare the QList type with Q_DECLARE_METATYPE first.

Example:


Qt Code:
  1. Q_DECLARE_METATYPE(QList<QString>)
  2.  
  3. class BookV2 {
  4. public:
  5. QString title;
  6. int pages;
  7. qreal rating;
  8. QList<QString> authors;
  9.  
  10. QT_MAP_STREAMABLE(
  11. QT_MAP_STREAMABLE_ITEM(QString,title)
  12. QT_MAP_STREAMABLE_ITEM(int,pages)
  13. QT_MAP_STREAMABLE_ITEM(qreal,rating)
  14. QT_MAP_STREAMABLE_ITEM(QList<QString> ,authors)
  15. )
  16. };
To copy to clipboard, switch view to plain text mode 


Author : Ben Lau
License: New BSD