Vladimir
16th January 2007, 10:25
I'm trying to use Q_PROPERTY as a quick way to represent my computational classes in GUI (and possibly to serialize them in XML). I've build simple model that shows all properties of the given object and it works well. But some of the properties are of custom types (fixed-length vectors etc.) and I want to show them too. Of course I can write code like this
if(p.userType() == qMetaTypeId<Vector2d>()) {
Vector2d v = p.read(_item).value<Vector2d>();
return QString("(%1, %2)").arg(v[0]).arg(v[1]);
} else if(p.userType() == ...)
but this makes adding new types harder, and I should duplicate this code in many places.
Later I've found that QVariant can serialize custom types to QDataStream using operator<< provided by type, but it provides only binary serialization. Are there any way to implement serialization to text ? Something like toString but which can handle user-defined types ?
if(p.userType() == qMetaTypeId<Vector2d>()) {
Vector2d v = p.read(_item).value<Vector2d>();
return QString("(%1, %2)").arg(v[0]).arg(v[1]);
} else if(p.userType() == ...)
but this makes adding new types harder, and I should duplicate this code in many places.
Later I've found that QVariant can serialize custom types to QDataStream using operator<< provided by type, but it provides only binary serialization. Are there any way to implement serialization to text ? Something like toString but which can handle user-defined types ?