Objects added to QList lose all of their member data

483 views Asked by At

I'm making a QList of a custom class called ControlIcon. I create the ControlIcons, and load them up with member variables, and then add them to the list. Here's the append code:

this->cueList.append(firstOne);

Here's the declaration of the QList:

QList< ControlIcon *> cueList;

If I break right after the append, I can see that the ControlIcon that was just added is full of members, and appears fine. I look in the list, and the ControlIcon that's been appended (and it does append a ControlIcon) has no members at all. I've made a QList of custom objects before, so I'm very confused. Could someone help?

3

There are 3 answers

0
CoutPotato On BEST ANSWER

I changed the data type of the list to be SerialController (the child of ControlIcon), which is what I was trying to add to it, and it works fine. This is really annoying, because there's other types of controllers, but I guess it will have to work for now.

0
Dr. Snoopy On

Your custom class must be assignable, that means it must provide a default constructor, a copy constructor and a assignment operator. If they don't, weird things like this can happen.

1
aschepler On

cueList contains pointers to ControlIcon objects, but it is not responsible for creating or keeping those ControlIcon objects. How did you get the pointer firstOne? If it points to something on the stack, that object would be invalid when you try to use it later. If it was created with new, it will stay valid until you clean it up using delete.