Results 1 to 3 of 3

Thread: QVariant::toString and custom data types

  1. #1
    Join Date
    Jan 2007
    Posts
    45
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default QVariant::toString and custom data types

    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

    Qt Code:
    1. if(p.userType() == qMetaTypeId<Vector2d>()) {
    2. Vector2d v = p.read(_item).value<Vector2d>();
    3. return QString("(%1, %2)").arg(v[0]).arg(v[1]);
    4. } else if(p.userType() == ...)
    To copy to clipboard, switch view to plain text mode 

    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 ?

  2. #2
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QVariant::toString and custom data types

    Quote Originally Posted by Vladimir View Post
    Something like toString but which can handle user-defined types ?
    Not very nice, but you could do something like this:
    (insert Pseudo-code/compile on own risk disclaimer here ;-)

    Header:
    Qt Code:
    1. namespace VariantConversion {
    2. QString (*VariantToStringFunction)(const QVariant &);
    3.  
    4. void registerVariantToStringFunction(int usertype,
    5. VariantToStringFunction function);
    6.  
    7. QString variantToString(const QVariant &variant);
    8. }
    To copy to clipboard, switch view to plain text mode 

    Implementation
    Qt Code:
    1. Q_GLOBAL_STATIC(QReadWriteLock, getLock)
    2. typedef QMap<int, VariantConversion::VariantToStringFunction> FunctionMap;
    3. Q_GLOBAL_STATIC(FunctionMap, getFunctionMap)
    4.  
    5. void VariantConversion::registerVariantToStringFunction(int usertype,
    6. VariantToStringFunction function)
    7. {
    8. QWriteLocker writeLock(&getLock());
    9. functionMap[usertype] = function;
    10. }
    11.  
    12. QString VariantConversion::variantToString(const QVariant &variant)
    13. {
    14. int type = variant.userType();
    15. if (type < QVariant::UserType) {
    16. return variant.toString();
    17. }
    18. QReadLocker readLock(&getLock());
    19. if (getFunctionMap().contains(type)) {
    20. return (getFunctionMap()[type])(variant);
    21. }
    22. qDebug("No registered function to convert Variant of type "
    23. "\"%s\" to QString", QMetaType::typeName(type));
    24. return QString();
    25. }
    To copy to clipboard, switch view to plain text mode 

    EDIT: I thought about it more, and having a lock in a static variable inside of a function is not the greatest idea, and kind of braindead, at least in older compilers.... (Look up thread safety of static variables)

    I do not think Q_GLOBAL_STATIC is public API at this point, but you can find information about it on the web:
    http://delta.affinix.com/2006/01/31/q_global_static/
    http://article.gmane.org/gmane.comp....vel.core/39118
    Last edited by camel; 16th January 2007 at 12:32.

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

    sunil.thaha (17th January 2007)

  4. #3
    Join Date
    Jan 2007
    Posts
    45
    Thanks
    5
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: QVariant::toString and custom data types

    Thanks a lot for quick reply ! You solution works good for me.

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.