Results 1 to 4 of 4

Thread: Using QVariant with custom data type

  1. #1
    Join Date
    Sep 2009
    Posts
    22
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Using QVariant with custom data type

    Hi all,
    I read the documentation about QVariant with the little example that I report here:

    Qt Code:
    1.  
    2. MyCustomStruct c;
    3. if (v.canConvert<MyCustomStruct>())
    4. c = v.value<MyCustomStruct>(v);
    5.  
    6. v = 7;
    7. int i = v.value<int>(); // same as v.toInt()
    8. QString s = v.value<QString>(); // same as v.toString(), s is now "7"
    9. MyCustomStruct c2 = v.value<MyCustomStruct>(); // conversion failed, c2 is empty
    To copy to clipboard, switch view to plain text mode 

    From this, I understand that I can initialize a QVariant variable only with data types supported by QVariant itself. Am I right?
    Is there a way to initialize the variable v with a custom type?

    Thanks for your anwers!
    Alex

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Using QVariant with custom data type

    did you use Q_DECLARE_METATYPE? and obviously an integer can't be converted to your struct...

    Eidt: misread your post. The good thing on QVariant is that you don't have to tell, which type it stores. You can check on runtime if it can be converted to one type you need.

    As to your question: you can just use Q_DECLARE_METATYPE and you can use your own type with QVariant.
    Last edited by Lykurg; 30th November 2009 at 13:30. Reason: updated contents

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

    QAlex (30th November 2009)

  4. #3
    Join Date
    Sep 2009
    Posts
    22
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QVariant with custom data type

    Ok! I'll consider using this technique to define my own type.
    I think it's the best solution for my porpouses.
    I'll try and make you know!

  5. #4
    Join Date
    Sep 2009
    Posts
    22
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using QVariant with custom data type

    Well... I tried and it works!!!
    I've created different kind of types and structures to use, but it works always!

    Very interesting functionality,
    Alex

Similar Threads

  1. Get custom data from qstandarditem?
    By alexandernst in forum Newbie
    Replies: 3
    Last Post: 11th August 2009, 03:24
  2. Convert between a custom data type wrapped in a QVariant
    By darkadept in forum Qt Programming
    Replies: 2
    Last Post: 17th March 2009, 09:07
  3. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 16:26
  4. QVariant custom/user type comparison
    By gri in forum Qt Programming
    Replies: 2
    Last Post: 12th August 2008, 14:36
  5. Compile 4.4.0
    By LordQt in forum Installation and Deployment
    Replies: 18
    Last Post: 29th May 2008, 13:43

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.