choosyg
choosyg

Reputation: 794

QVariant with std::shared_ptr

i have the following problem, i am using

Q_DECLARE_METATYPE( std::shared_ptr<int> );
qRegisterMetaType< std::shared_ptr<int> >();
QMetaType::registerComparators< std::shared_ptr<int> >();

to use std::shared_ptr<int> with e.g. QListModel. I need a behavior where

QVariant::fromValue( std::shared_ptr<int>( new int(5) ) ) == QVariant::fromValue( std::shared_ptr<int>( new int(5) ) )

is true. My code above return false here since std::shared_ptr<int>::operator== () compares the raw pointers. is it possible to register comparators other than the standard operators in QMetaType::registerComparators?

Upvotes: 7

Views: 5295

Answers (1)

John Zwinck
John Zwinck

Reputation: 249153

You could try using registerConverter() to allow implicit conversion of the shared_ptr<int> to a regular int, and compare them that way. Obviously then you would not do registerComparator(). An alternative would be to wrap shared_ptr<int> in your own class and implement comparison the way you want.

Or check out Q_DECLARE_SMART_POINTER_METATYPE.

Upvotes: 6

Related Questions