Results 1 to 4 of 4

Thread: Storing boost.shared_ptr in a QVariant

  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 21:11. Reason: reformatted to look better

  2. #2
    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: Storing boost.shared_ptr in a QVariant

    Qt Code:
    1. boost::shared_ptr<int> smartInt(new int(5));
    2. QVariant smartVariant = QVariant::fromValue(smartInt);
    3. QVariant smartVariant2 = QVariant::fromValue(smartInt);
    4. equal = smartVariant2 == smartVariant;
    5. boost::shared_ptr<int> a = smartVariant.value<boost::shared_ptr<int> >();
    6. boost::shared_ptr<int> b = smartVariant2.value<boost::shared_ptr<int> >();
    7. qDebug() << a.get() << b.get();
    8. qDebug() << a.use_count() << b.use_count();
    To copy to clipboard, switch view to plain text mode 
    QVariant has made a copy of your shared_ptr in each of smartVariant and smartVariant2. They are two different smart_ptr objects (with no operator==) so the equality test fails. They do however point to the same shared data (debug line 1). Debug line 2 shows that the shared data is more shared than you think: a reference count of 5 (from smartInt, the two variants, a, and b).

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

    Default Re: Storing boost.shared_ptr in a QVariant

    Thanks, Chris. I wonder if I could implement operator== to get the behavior I want....

  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: Storing boost.shared_ptr in a QVariant

    I just revisited the Boost shared_ptr docs. boost::shared_ptr::operator==() does exist and compares the raw pointers as you would expect. The behaviour you are seeing in QVariant is a limitation of the QVariant::operator==() : "In the case of custom types, their equalness operators are not called. Instead the values' addresses are compared." You may be stuck with manually extracting the two shared_ptrs and using them directly in the equality test.

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, 08:40
  2. QSharedPointer vs Boost::tr1::shared_ptr()
    By photo_tom in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2010, 17:48
  3. Replies: 1
    Last Post: 4th December 2009, 18:03
  4. Flag storing
    By bismitapadhy in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2009, 06:26
  5. QVariant - storing QIcons[]
    By moowy in forum Qt Programming
    Replies: 7
    Last Post: 25th August 2006, 01: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.