lundi 20 avril 2015

Best practice when accesessing derived classes in a QList

I have a base model where I have implemented the virtual members of QAbstractItemModel. I then use my base model in my project as needed by deriving new classes with specifics.

class BaseModel : public QAbstractItemModel{
    public:
        ...

    protected:
        QList<BaseItem*> list;
}

class DerivedModel : public BaseModel{
    public:
        ...
}

class DerivedItem : public BaseItem{
    public:
        ...
}

My derived model uses DerivedItem objects to store data which have some specifics that BaseItem doesn't have. They are stored in list. Methods in BaseModel uses objects in list as well.

My issues is because of this I have to type cast every time I access objects from list in the derived model. And I can't use macros like foreach.

Is there any tips or tricks I can use in this circumstance that will enable me to use macros and prevent me from type casting every time I access items from the list. Or is there another method (better practice) when making a common class to later derive from.

Thanks,

Aucun commentaire:

Enregistrer un commentaire