Store QWeakPointers in QSet
Hi,
I'm trying to store QWeakPointers in a QSet with no success. The compiler first complained about QWeakPointer is lacking a qHash function. I Implemented a qHashFunction, the project compiled but I can't fetch data from the QSet (tried iterators). It only returns invalid QWeakPointers.
Basically I'm trying to do:
Code:
QSet<QWeakPointer<MyClass> > oneSet;
QSharedPointer<MyClass> > myClass(new MyClass);
oneSet.insert(oneSet.toWeakRef());
for (QSet<QWeakPointer<MyClass> >::iterator it = oneSet.begin(); it != oneSet.end(); it++)
{
// do stuff
}
//My qHash:
template<typename T>
uint qHash(const QWP<T> &p)
{
return qHash(p.data()); //use the pointer a a key.
}
Does anyone have a solution for this?
Re: Store QWeakPointers in QSet
Can you explain what the following line is supposed to do?
Code:
oneSet.insert(oneSet.toWeakRef());
Re: Store QWeakPointers in QSet
That is a typo. It should be:
Code:
oneSet.insert(myClass.toWeakRef());
I chose to use QLinkedList instead of QSet. But I think I found the problem was. I believe that the qHash function was working, but I forgot to keep a QSharedPointer to the created objects. When the QSharedPointer went out of scope all the newly created objects were deleted, and only left the weak pointers behind. When I tried to iterate the list it couldn't promote the weak pointer to a strong pointer.
Re: Store QWeakPointers in QSet
Why not have a QList<QSharedPointer<MyClass> >?
Re: Store QWeakPointers in QSet
I need to have QWeakPointer because of cyclic reference. MyClass holds a QSharedPointer to a child MyClass and a QWeakPointer to a parent MyClass.
Re: Store QWeakPointers in QSet
And where is this cyclic reference?