Results 1 to 4 of 4

Thread: Storing boost.shared_ptr in a QVariant

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2009
    Posts
    19
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Storing boost.shared_ptr in a QVariant

    Hello all. I want to store smart pointers in QVariants for purposes of working with Qt's model/view system. However, it appears that QVariant.operator== doesn't work when the type is a smart pointer. For example:

    Qt Code:
    1. #include <boost/shared_ptr.hpp>
    2. Q_DECLARE_METATYPE(boost::shared_ptr<int>);
    3. Q_DECLARE_METATYPE(int*);
    4.  
    5. void testQVariant()
    6. {
    7. int* rawInt = new int(5);
    8. QVariant rawVariant = QVariant::fromValue(rawInt);
    9. QVariant rawVariant2 = QVariant::fromValue(rawInt);
    10. bool equal = rawVariant2 == rawVariant;
    11.  
    12. boost::shared_ptr<int> smartInt(new int(5));
    13. QVariant smartVariant = QVariant::fromValue(smartInt);
    14. QVariant smartVariant2 = QVariant::fromValue(smartInt);
    15. equal = smartVariant2 == smartVariant;
    16. }
    To copy to clipboard, switch view to plain text mode 

    The QVariants are equal when using raw pointers, but not when using smart pointers (boost::shared_ptr in this case, but I see the same behavior with another smart pointer type, as well).

    The upshot is that I can store these in a model (as Qt::UserRole data), but I'm unable to ask the model to find them (match() always returns an index of -1). Any ideas?
    Last edited by negritot; 28th September 2010 at 20:11. Reason: reformatted to look better

Similar Threads

  1. Storing and reading back template types in QVariant
    By Daniel Dekkers in forum Qt Programming
    Replies: 1
    Last Post: 5th April 2010, 07:40
  2. QSharedPointer vs Boost::tr1::shared_ptr()
    By photo_tom in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2010, 16:48
  3. Replies: 1
    Last Post: 4th December 2009, 17:03
  4. Flag storing
    By bismitapadhy in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2009, 05:26
  5. QVariant - storing QIcons[]
    By moowy in forum Qt Programming
    Replies: 7
    Last Post: 25th August 2006, 00:41

Tags for this Thread

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.