Results 1 to 7 of 7

Thread: Avoiding Q_DECLARE_METATYPE

  1. #1
    Join Date
    Feb 2013
    Posts
    38
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Avoiding Q_DECLARE_METATYPE

    Hi,

    I have a standard C++ object.

    Qt Code:
    1. class A
    2. {
    3. // ...
    4. };
    To copy to clipboard, switch view to plain text mode 

    I can't (and I don't want to) define it as a Qt type.

    Though, I still like to put it in the model (QStandardItemModel for instance).

    So far, I was able to but a raw pointer into the model with the very ugly following code :

    Qt Code:
    1. // I have
    2. const A &a = ; //...
    3.  
    4. // Writing
    5. item->setData(
    6. qVariantFromValue(
    7. reinterpret_cast<void*>(
    8. const_cast<A*>(
    9. &a
    10. )
    11. )
    12. ),
    13. Role
    14. );
    15.  
    16. // Reading
    17. const A &a = *reinterpret_cast<A*>(
    18. const_cast<const void*>(
    19. item->data(Role).value<void*>()
    20. )
    21. );
    To copy to clipboard, switch view to plain text mode 

    How bad is this ?

    Anything better ?

    Thanks !

    PS0 : I read the method with Q_DECLARE_METATYPE.
    PS1 : I could always create an intermediate Qt type which would embed my non Qt type...but that seems heavy too.
    Last edited by moijhd; 10th July 2013 at 20:17.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Avoiding Q_DECLARE_METATYPE

    Would this look simpler
    Qt Code:
    1. ...
    2. ...
    3. const A a0;
    4. ...
    5. item.setData(reinterpret_cast<unsigned int>(&a), Role);
    6. ...
    7. const A & a1 = *reinterpret_cast<const A*>(item.data(Role).toUInt());
    8. //or
    9. const A & a2 = *reinterpret_cast<const A*>(modelIndex.data(Role).toUInt());
    10. ...
    To copy to clipboard, switch view to plain text mode 
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Feb 2013
    Posts
    38
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Avoiding Q_DECLARE_METATYPE

    Okay for the casts factorisation, and the "toUInt()" simplification.

    What the difference between the "void*" and the "unsigned int" casts ?

    I suppose most machines will accept them indifferently. Though, I might have to mind about portability. So I guess the safest move id to go with "void*" ? But "unsigned int" simplifies a lot

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Avoiding Q_DECLARE_METATYPE

    Why avoid the macro and invent work that can be hidden in the QVariant class for you?

  5. #5
    Join Date
    Feb 2013
    Posts
    38
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Avoiding Q_DECLARE_METATYPE

    What if I have no control over the type ie the type is defined in a library ? I have to create a header include the library and including the Q_DECLARE_METATYPE and I will link that file when I need the meta type ? Fair enough.

    By the way, can we guard Q_DECLARE_METATYPE through Qt ?
    Last edited by moijhd; 11th July 2013 at 08:50.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Avoiding Q_DECLARE_METATYPE

    Quote Originally Posted by moijhd View Post
    What if I have no control over the type ie the type is defined in a library ?
    If the type has a default constructor and can be copied then you can store it in a QVariant.
    If the class does not meet the requirements then you can store a pointer (const or not) in a QVariant.
    You need to arrange somewhere that Q_DECLARE_METATYPE() is invoked before you try to use the 3rd party type in a QVariant.

    Qt Code:
    1. #include <QtCore>
    2.  
    3. // From some library header
    4. class AClass {
    5. public:
    6. AClass() { }
    7. ~AClass() { }
    8. AClass(const AClass &other) { }
    9. };
    10.  
    11. class BClass {
    12. public:
    13. BClass() { }
    14. ~BClass() { }
    15. private:
    16. BClass(const BClass &other) { } // cannot be copied
    17. };
    18. // end of header
    19.  
    20. Q_DECLARE_METATYPE(AClass)
    21. Q_DECLARE_METATYPE(const BClass*)
    22.  
    23. int main(int argc, char **argv) {
    24. QCoreApplication app(argc, argv);
    25.  
    26. AClass aa;
    27. QVariant av = QVariant::fromValue(aa);
    28. // later
    29. AClass ar = av.value<AClass>();
    30.  
    31. const BClass bb;
    32. QVariant bv = QVariant::fromValue(&bb);
    33. // later
    34. const BClass *br = bv.value<const BClass*>();
    35.  
    36. return 0;
    37. }
    To copy to clipboard, switch view to plain text mode 

    By the way, can we guard Q_DECLARE_METATYPE through Qt ?
    I have no idea what this means.

  7. #7
    Join Date
    Feb 2013
    Posts
    38
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Avoiding Q_DECLARE_METATYPE

    Ok thanks !

    Guard for me

    Qt Code:
    1. #ifndef Q_DECLARE_METATYPE_AClass
    2. #define Q_DECLARE_METATYPE_AClass
    3. Q_DECLARE_METATYPE(AClass)
    4. #endif
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Q_DECLARE_METATYPE() questions.
    By hickscorp in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2010, 16:04
  2. Problem to declare a stl map - Q_DECLARE_METATYPE
    By Althor in forum Qt Programming
    Replies: 5
    Last Post: 9th December 2008, 09:41
  3. How to use Q_DECLARE_METATYPE
    By lni in forum Qt Programming
    Replies: 6
    Last Post: 21st July 2008, 17:13
  4. Q_DECLARE_METATYPE and qVariantCanConvert
    By manojmka in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2008, 06:13
  5. Replies: 2
    Last Post: 12th December 2007, 13:15

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.