PDA

View Full Version : QList, copy problems



Valheru
19th August 2008, 08:38
I am having trouble with an application that uses QLists to store pointers to objects. I create a QList of pointers to objects in a function, and return it. In the destructor of each object (which is in turn a QList of pointers to objects) I have the standard qDeleteAll(); clear(); rigmarole.
Now, whenever I use that function (it's loading an XML file off disk) to provide a QList which I use to populate a QAbstractModelItem implementation, the program crashes with a SIGSEV pointing to the destructor of the object. This would seem to point to the returned QList going out of scope, and deleting it's container. Then when the model comes around to accessing the data that it thinks it can, the data pointed to by it's internal QList has been deleted, since it is after all an array of pointers. This behaviour isn't exhibited BTW if I comment out the qDeleteAll(); clear(); in the destructor.
I am trying to write the classes contained in the QList to have deep copy constructor, so that when a copy does take place then the data is actually copied, and not just the pointers. I may be going about this in the wrong way, but I made a minimal program to demonstrate what I am trying to do. If you run it, you can see that the copy constructors are never called. My question is, what is neccessary to do to get the QList to actually make deep copies of the data it contains when the QList contains pointers? Or am I going about this the wrong way, and should I just store the objects by value?

You can compile the program using CMake >= 2.4

TIA,
Valheru

jpn
19th August 2008, 08:41
If you have a list of pointers, the list copies pointer values, not objects.

aamer4yu
19th August 2008, 09:36
My question is, what is neccessary to do to get the QList to actually make deep copies of the data it contains when the QList contains pointers?

You might know the class or type of object the pointers are ? isnt it ? When copying the list, make new objects and copy all the member variables of that object. the memer variables i guess is the data u need to make a copy, isnt it ?

Valheru
19th August 2008, 10:04
Hmm, the reason for the crash turns out to be rather different. I was implementing QVariant operators for my classes as per the wiki, so that I could get rid of the QVariant::fromValue( *myObject ) calls. I also put a qDebug() statement in the copy constructor of my class. Then, when calling setData( QModelIndex idx, *myObject ), the debug messages showed that the copy constructor was being called recusively, despite being defined as MyClass::MyClass( const MyClass &other ). This was obviously calling the program to crash. It wasn't even entering the setData() function, as I had qDebug() statements in there to check. It just spins in a loop, making copies of *myObject.

I am implementing copy constructors as I need the parents to changed when that object it copied, otherwise the QAbstractItemModels can't keep track of tree heirarchy. I am a bit lost as to why this is happening though :confused:

Nightfox
5th February 2010, 00:06
Hi Valheru.

I've looked at your post and your example code as I'm experiencing the same problems as you did. I your post it seems like you've solved the copy problem? I'm fighting to get my copy function to work and I've ended up doing a deep copy by implementing a clone function, like this. I'm using a QMap instead of your QList.
Item Item::clone()
{
Item itm;
QMap<int,QVariant>::const_iterator i = this->constBegin();
while (i != this->constEnd()) {
itm[i.key()] = i.value();
++i;
}
return itm;
}

If it's not too much trouble, will you share with me your solution from above?

Thanks