Results 1 to 7 of 7

Thread: Avoiding Q_DECLARE_METATYPE

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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
  •  
Qt is a trademark of The Qt Company.