QVariant with std :: shared_ptr

I have the following problem: I use

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

use std::shared_ptr<int>for example. QListModel. I need a behavior in which

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

truly. My code above returns false here, as it std::shared_ptr<int>::operator== ()compares the source pointers. Is it possible to register comparators other than standard operators in QMetaType::registerComparators?

+4
source share
1 answer

You can try using registerConverter () to enable the implicit conversion of shared_ptr to a regular int and compare them this way. Obviously, you will not do registerComparator (). An alternative would be to wrap shared_ptr in your own class and implement the comparison the way you want.

Q_DECLARE_SMART_POINTER_METATYPE.

+3

Source: https://habr.com/ru/post/1530257/


All Articles