[Qt-interest] QSharedPointer and QMap/QHash
Dan Milburn
danmilburn at clara.co.uk
Fri Feb 12 15:36:18 CET 2010
Hi,
The following works for me:
template <class T>
uint qHash( const QSharedPointer<T> &p )
{
return qHash( p.data() );
}
Dan
> Thank you for reply, But It's not tiny way to implement qHash for every type of QSharedPointer<Class1>, QSharedPointer<Class2> and so on. Is it only way?
>
>
>> Hi,
>>
>> Indeed, QHash can only handle keys with a class that has a qHash
>> function already. As you can see
>> (http://doc.trolltech.com/4.5/qhash.html) there are already a number of
>> such functions for the mose common types. You'll have to define your own
>> qHash function taking a QSharedPointer as argument and returning a uint.
>> You can for example return the result of qHash(sharedPointer.data())
>> since there is a qHash function for the pointer type.
>>
>> I hope this can help you.
>> Regards,
>>
>> Alex
>>
>>
>> Rudenko Eugene a écrit :
>>> Hello.
>>> I'm trying to use QMap/QHash with QSharedPointer with no success.
>>>
>>> For example, such code:
>>> ===============================
>>> class MyClass : QObject {
>>> Q_OBJECT
>>> ..........
>>> QMap< QSharedPointer<MyClass>, int > mm; //or QHash
>>> }
>>>
>>>
>>> ........
>>> QSharedPointer<MyClass> a = b.getPointer();
>>> mm.insert(a,0);
>>>
>>> ===============================
>>> I have got error:
>>> in case of use QHash:
>>> no matching function to call to 'qHash(const QSharedPointer<MyClass> & )'
>>> candidates: ..........
>>>
>>>
>>> in case of use QMap:
>>> no match for 'operator<' in 'key1 < key2'
>>> candidates are ........
>>>
>>>
>>> As I can see, there are no overloaded operators "<" and "qHash" for QSharedPointer, to redirect it to containing object.
>>> As I can understand, QSharedPointer is not suitable to use with QMap and QHash, Is I right?
>>> How can I use container classes with smart pointer classes ?
>>>
>>>
>>> With best regards, Eugene Rudenko.
>>>
>>>
>>>
>>>
>>>
>>> _______________________________________________
>>> Qt-interest mailing list
>>> Qt-interest at trolltech.com
>>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>>>
>>>
>>
>> --
>> Alexandre BERAUD
>> Ingénieur Développement
>>
>> Infflux - Informatique & Flux
>> Tel: 01 49 57 92 00 - Fax : 01 49 57 92 01
>> Mail: aberaud at infflux.com
>> Visitez notre site : www.infflux.com
>>
>> _______________________________________________
>> Qt-interest mailing list
>> Qt-interest at trolltech.com
>> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
More information about the Qt-interest-old
mailing list