Results 1 to 3 of 3

Thread: Convert between a custom data type wrapped in a QVariant

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2006
    Posts
    70
    Thanks
    12
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Convert between a custom data type wrapped in a QVariant

    I have a custom class called Money that I want stored in a QVariant. I then want to take that QVariant and convert it to and from a QVariant of type double.

    Here is my custom Money class:
    Qt Code:
    1. class Money {
    2. public:
    3. Money(double value = 0.00) {
    4. _val = value;
    5. }
    6. ~Money() { }
    7. double toDouble() {
    8. return _val;
    9. }
    10. operator Double() {
    11. return toDouble();
    12. }
    13. private:
    14. double _val;
    15. };
    16.  
    17. Q_DECLARE_METATYPE(Money);
    To copy to clipboard, switch view to plain text mode 

    I'm using Qt's property system (which stores QVariants) but I'll just simplify everything here:
    Qt Code:
    1. //Create a QVariant of type double
    2. double myDouble = 2.345;
    3. QVariant doubleVariant;
    4. doubleVariant.setValue(myDouble);
    5.  
    6. //Create a QVariant of type Money
    7. QVariant moneyVariant;
    8. moneyVariant.setValue(Money(0.00));
    9.  
    10. /*This is where I'm bogged down.
    11. I have a QVariant of type double that holds the real value.
    12. I also have an empty QVariant of type Money.
    13. I want to be able to transfer the value from doubleVariant to
    14. moneyVariant without knowing about the Money class or double type.
    15. I also want to preserve what type the destination QVariant is.*/
    16.  
    17. //Attempt to convert from the double variant to the money variant
    18. moneyVariant = QVariant::fromValue(doubleVariant);
    19. Money myMoneyValue = moneyVariant.value<Money>();
    20.  
    21. //Obviously the above two lines doesn't work.
    22. //Instead of moneyVariant being a QVariant of type Money it is now
    23. //a QVariant of type double. Also the conversion back to Money doesn't work.
    To copy to clipboard, switch view to plain text mode 

    Any ideas? I will try to clarify what I'm trying to do if that doesn't make sense.

    Thanks,
    Mike
    Last edited by darkadept; 16th March 2009 at 23:00.

Similar Threads

  1. Custom Model Advice Requested
    By mclark in forum Qt Programming
    Replies: 3
    Last Post: 18th September 2008, 16:26
  2. QVariant custom/user type comparison
    By gri in forum Qt Programming
    Replies: 2
    Last Post: 12th August 2008, 14:36
  3. Compile 4.4.0
    By LordQt in forum Installation and Deployment
    Replies: 18
    Last Post: 29th May 2008, 13:43
  4. dummy question(Error)
    By Masih in forum Qt Programming
    Replies: 12
    Last Post: 19th July 2007, 23:38
  5. How to convert binary data to hexadecimal data
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 8th March 2006, 16:17

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.