Important: Please read the Qt Code of Conduct -

Adding derived objects to a list dynamically

  • Hi ...
    I have a situation:
    I have a abstract class from QObject and base class which is derivated from the abstract. There are approximately 30 classes which are derivated from the base class. These classes are all singleton and they will be constructed within project. I want to have list of these singleton objects always. I don't want to make hard code instead i want to save and access them from base class. I have tried to create a static list in base class and add objects to this list in constructor of base class but it fails and gives and error. I'm not much expert at object oriented design with combination of static members! If someone can help me i'll be appreciated.

    If i can have a static method to return list of derivated objects, that would be great but i don't know how?! May be it's one of design patterns which i don't know...

  • I don't think the problem is that static.
    So how are you creating the list?
    I ask this because you can't have a QList<QObject> for two reasons: QObject and the classes derived from QObject are not copyable, and second reason is that objects are not polymorphic (if you copy a Derived object into a Base object you slice off the Derived part), to solve both the problems you must use a QList of pointers to your objects: QList<QObject*>

  • Ok, Can i add item's pointer in constructor of base class? then how?

Log in to reply