I need to make multiple instances of various class types (classA, classB, classC), and store then in a single list. Is is possible to create these objects dynamically and then add them to a single QList? Something like:
QList<QObject> mylist;
mylist.append(classA());
mylist.append(classB());
mylist.append(classC());
This won't compile because the objects are not of type QObject. I could static_cast them to QObject, but will I be able to typeid.name() to determine their origical types? (To cast them back later)?
I found an example here of using a QList< QVariant >, but the compiler complains that:
error: no matching function for call to 'QVariant::QVariant(classA&)'
for this code:
QList<QVariant> mylist;
mylist.append(QVariant(tempObj));
Update:
I got it compiling by creating the object during the append operation, AND using ::fromValue.
mylist.append(QVariant::fromValue(ClassA(1,2,3)));
Not sure this is right. 1 - will this cause a memory leak/problem creating the object in the append statement? 2 - why would I need ::fromValue to make this work? (Does this just make a second copy of the object?)
I put a qDebug in my constructors, and I see that my constructor with parameters is called twice, then the copy constructor is called once, and the default (no parameters) constructor is never called. I don't understand that...
std::list<QObject*>orstd::list<std::unique_ptr<Object>>, since the objects are notQObjects? - PaulMcKenzieQList::appendthat takes aClassA. - It's Your App LLC