Results 1 to 5 of 5

Thread: Use QVariant with custon data types

  1. #1
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Use QVariant with custon data types

    Hi guys,

    I need to use QVariant with custom data type (i.e. in QComboBox I can associate a box entry with data).
    How can I do?

    Thanks
    A camel can go 14 days without drink,
    I can't!!!

  2. #2
    Join Date
    Jan 2006
    Posts
    32
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Use QVariant with custon data types

    from http://doc.trolltech.com/4.1/qvariant.html:

    A Note on GUI Types
    Because QVariant is part of the QtCore library, it cannot provide conversion functions to data types such as QColor, QImage, and QPixmap, which are part of QtGui. In other words, there is no toColor() function.
    Instead, you can use the QVariant::value() or the qVariantValue() template function. For example:
    QVariant variant;
    ...
    QColor color = variant.value<QColor>();
    The inverse conversion (e.g., from QColor to QVariant) is automatic for all data types supported by QVariant, including GUI-related types:
    QColor color = palette().background().color();
    QVariant variant = color;

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Use QVariant with custon data types

    AFAIK you can't add custom datatypes to QVariant. You may only store custom data within one of the existing types (like a string or an int) like shown in the second post.

  4. #4
    Join Date
    Jan 2006
    Posts
    32
    Thanks
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Use QVariant with custon data types

    Quote Originally Posted by wysota
    AFAIK you can't add custom datatypes to QVariant. You may only store custom data within one of the existing types (like a string or an int) like shown in the second post.
    you can store whatever you want, in fact QColor isn't supported natively (there's no toColor() method), because it's not part of QtCore.

    you can use it this way:

    MyType t;
    QVariant var = t;
    ...
    MyType t2 = var.value<MyType>();

  5. #5
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use QVariant with custon data types

    Quote Originally Posted by Dusdan
    you can store whatever you want, in fact QColor isn't supported natively (there's no toColor() method), because it's not part of QtCore.

    you can use it this way:

    MyType t;
    QVariant var = t;
    ...
    MyType t2 = var.value<MyType>();
    You can do this only for Registered types otherwise you obtain a compiler error
    You can register a custom type in this way
    Qt Code:
    1. Q_DECLARE_METATYPE(MyType);
    To copy to clipboard, switch view to plain text mode 

    If you want to register also a type name you can use
    Qt Code:
    1. int myTypeId = qRegisterMetaType<MyType>("MyType")
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. conversion of binay data to qt types
    By rajeshs in forum Qt Programming
    Replies: 16
    Last Post: 4th January 2008, 12:26
  2. system-independent C++ data types
    By magland in forum General Programming
    Replies: 15
    Last Post: 28th March 2007, 20:33
  3. QVariant::toString and custom data types
    By Vladimir in forum Qt Programming
    Replies: 2
    Last Post: 16th January 2007, 15:36
  4. speed of setdata - lots of items in treeview
    By Big Duck in forum Qt Programming
    Replies: 4
    Last Post: 6th July 2006, 12:53
  5. QVariant types support for custom properties
    By Dusdan in forum Qt Tools
    Replies: 9
    Last Post: 11th January 2006, 09:55

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.