Objects added to QList lose all of their member data
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?
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.
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
.
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.
精彩评论