PDA

View Full Version : Query about QHash , QList



aamer4yu
14th December 2006, 07:41
I want to know if QHash, QList makes a copy of the items inserted into them or what ??
Suppose I have a function CreateList()



class MyNode
{
int id;
QScene *scene; // pointer to a scene or any other class based on id.
public :
MyNode(QScene *s)
{ scene = s;
id = 0; // or some unique value
}
};
QList<MyNode> list;
void CreateList()
{
MyNode node(this->pScene);
list.append(node);
}


Here I have added just one item, though multiple items may be insterted in the list. I want to know when CreateList function is exited, wll list still have valid copy of node ??

also what if i have pointers instead of objects..
like


QList<MyNode*> list;
void CreateList()
{
MyNode *node = new MyNode(this->pScene); // node contains some
list.append(node);
}

in this case how do i delete the actual nodes created. Does list.clear() call the destructor too ?? if not what is the safe way of freeing the memory when a list of pointers is cleared ?

I think I know the answers, but still a little confused,so asking in the forum, (i know i sound a little stupid here :D)
Thx in advance

e8johan
14th December 2006, 08:25
Lists copies, so if you do not want that - use a pointer list. That way it is the pointer and not the object that is copied.

lauranger
14th December 2006, 09:20
Hi
cf qDeleteAll http://doc.trolltech.com/4.2/qtalgorithms.html#qDeleteAll-2
Hth

aamer4yu
14th December 2006, 10:04
Thanks to both johan and lauranger :)