Search code examples
c++qtcastingqlistqmap

Retrieve differents Qmap in a single variable


I am working on a game in Qt. My characters/objects are stored in my model Class (I try to follow the MVC model).

I created a QMap containing for each of the object :

QMap<int, Safe*> *safes;
QMap<int, Mushroom*> *mushroom;
QMap<int, Floor*> *floors;

But then I would like to retrieve all theses QMap in my Controller and send it to the paintEvent() class of my View from the controller. Is there a way to store the QMap in a QList like this :

QList<QMap<int, void*>>

And then cast it ? I am searching for a way to acces to theses QMap from a single object.

Thank you for your help !


Solution

  • You could use a struct to bundle them up inside one object:

    struct Maps
    {
        QMap<int, Safe*> *safes;
        QMap<int, Mushroom*> *mushroom;
        QMap<int, Floor*> *floors;
    };
    

    Although it's valid to have a pointer to QMap, if you don't need to hold a pointer to it then I would advise against it.

    struct Maps
    {
        QMap<int, Safe*> safes;
        QMap<int, Mushroom*> mushroom;
        QMap<int, Floor*> floors;
    };
    

    That way you don't have to worry about heap allocations/deallocations.

    If you have a compiler that supports C++11 then you can use std::tuple to group items together.

    std::tuple<QMap, QMap, QMap> maps (safes, mushroom, floors);