Should I use QList<MyClass> or QList<MyClass *>? And why doesn't QList support abstract classes? Isn't that an important feature?
I am developing a subclass of QList<MyClass *> called "stack" which adds some extra operations on its objects and is also an item model so it can be viewed. These operations also save a copy of the list into a list of QList<MyClass *>. But when I try to use recalled list, the pointers are invalid! 
Some members of Stack:
QList<QList<DElement*>*> history;
void undo();
QList<DElement> * duplicate();
void nextHistoryStep();
QList<QList<DElement*>*> history;
void undo();
QList<DElement> * duplicate();
void nextHistoryStep();
To copy to clipboard, switch view to plain text mode
nextHistoryStep is called when an operation is complete. The current state is appended to history. Or so I think
undo is supposed recall the previous state by copying the pointers back.
Here are the functions:
void Stack::nextHistoryStep()
{
history.append(duplicate());
}
QList<DElement> * Stack::duplicate()
{
QList<DElement*> * now = new QList<DElement*>();
for(int i = 0; i < size(); i++)
{
now->append(at(i));
}
return now;
}
void Stack::undo()
{
if(history.size() > 1)
{
this->clear();
// reload from last history
QList<DElement*> * prev = history.at(history.size() - 2);
for(int i = 0; i < prev->size(); i++)
{
append(prev->at(i));
}
emitUpdate();
history.removeLast();
delete prev;
}
}
void Stack::nextHistoryStep()
{
history.append(duplicate());
}
QList<DElement> * Stack::duplicate()
{
QList<DElement*> * now = new QList<DElement*>();
for(int i = 0; i < size(); i++)
{
now->append(at(i));
}
return now;
}
void Stack::undo()
{
if(history.size() > 1)
{
this->clear();
// reload from last history
QList<DElement*> * prev = history.at(history.size() - 2);
for(int i = 0; i < prev->size(); i++)
{
append(prev->at(i));
}
emitUpdate();
history.removeLast();
delete prev;
}
}
To copy to clipboard, switch view to plain text mode
Deleting a QList destroys that contents too. Is that just the internal pointers (in this case a pointer to pointer) or my actual objects? This code runs without a problem-but the instant that the contents are actually accessed, a segfault occurs. It is possible to delete the unneeded QList without destroying the contents and is this the problem?
Oh, and DElement was originally abstract. It's a generic "element." But this caused a weird compiler error.
Bookmarks