Results 1 to 3 of 3

Thread: using custom datatypes with QVariant/Q_DECLARE_METATYPE();

  1. #1
    Join Date
    Dec 2007
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default using custom datatypes with QVariant/Q_DECLARE_METATYPE();

    Hello everyone,
    I want to use a custom data type with QVariant, specifically I want to push_back a "Vector3" class instance onto a QVector<QVariant>. I already searched the forums and found some helpful information ( for example http://www.qtcentre.org/forum/f-qt-p...types-146.html ), but somehow it doesn´t work the way it should.

    my "Vector3" class:
    Qt Code:
    1. #ifndef __VECTOR3_H_
    2. #define __VECTOR3_H_
    3.  
    4. #include <QMetaType>
    5.  
    6. class Vector3
    7. {
    8.  
    9. public:
    10. Vector3(): x(0), y(0), z(0) {};
    11.  
    12. Vector3(float inX, float inY , float inZ) : x (inX), y (inY), z (inZ) {};
    13.  
    14.  
    15. Vector3::Vector3(const Vector3 &vector3) {
    16. x=vector3.getX();
    17. y=vector3.getY();
    18. z=vector3.getZ();
    19. }
    20.  
    21. [...]
    22.  
    23. private:
    24. float x;
    25. float y;
    26. float z;
    27.  
    28. };
    29.  
    30. Q_DECLARE_METATYPE(Vector3);
    31.  
    32. #endif //__VECTOR3_H_
    To copy to clipboard, switch view to plain text mode 

    I try to use it that way (parameters is: QVector<QVariant> parameters; ):
    Qt Code:
    1. Vector3 test;
    2. parameters.push_back(test);
    To copy to clipboard, switch view to plain text mode 

    Which results in the following:
    .\view\Drawing.cpp(20) : error C2664: 'QVector<T>:ush_back' : cannot convert parameter 1 from 'Vector3' to 'const QVariant &'
    with
    [
    T=QVariant
    ]
    Reason: cannot convert from 'Vector3' to 'const QVariant'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
    Now my naive understanding was that I´d only have to Q_DECLARE_METATYPE(Vector3) and everything would work, but that´s obviously wrong. A few pointers in the right direction would be greatly appreciated.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: using custom datatypes with QVariant/Q_DECLARE_METATYPE();

    See article [WIKI]Using custom data types with Qt[/WIKI]. You will have to use QVariant::fromValue() or to implement a QVariant() operator.
    J-P Nurmi

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

    SK. (12th December 2007)

  4. #3
    Join Date
    Dec 2007
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: using custom datatypes with QVariant/Q_DECLARE_METATYPE();

    Thanks a lot, that did the trick.

Similar Threads

  1. Custom proxy model issue
    By Khal Drogo in forum Qt Programming
    Replies: 13
    Last Post: 30th November 2007, 13:41
  2. custom plug-in widget in another custom plug-in widget.
    By MrGarbage in forum Qt Programming
    Replies: 6
    Last Post: 27th August 2007, 16:38
  3. Replies: 1
    Last Post: 5th March 2007, 21:50
  4. Replies: 16
    Last Post: 7th March 2006, 16:57
  5. Replies: 4
    Last Post: 2nd March 2006, 00:11

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.