1

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 !

ltheron
  • 301
  • 1
  • 3
  • 12

3 Answers3

3

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);
Mohamad Elghawi
  • 2,071
  • 10
  • 14
  • Perhaps using a class would be better. I assume that if he wants to group them somehow he will want to apply operations. Having a class would probably be better for this reason no? +1 for the pointer to QMap – LBes Oct 15 '15 at 08:36
  • By the way, there's something called `std::tuple` if you want to be lightweight and convenient :) – yzn-pku Oct 15 '15 at 08:40
  • 1
    @LBesancon As you know in C++ the only difference between a struct and a class is their default access specifiers and the way they are inherited. So you could have methods in your struct if you wanted to. I do however tend to use structs when I need to group some data together without any methods but it's completely down to you. – Mohamad Elghawi Oct 15 '15 at 08:42
  • @yzn-pku You're absolutely right. I will update my answer. – Mohamad Elghawi Oct 15 '15 at 08:43
  • @MohamadElghawi sure you can have methods in your struct. It is more a matter of what you're used to do. Basically I tend to create classes whenever the need arises but you're right. Did not downvote your answer anyway for that ;) – LBes Oct 15 '15 at 08:46
1

First, yes you can use a QList for this purpose, however I would suggest to create an interface class first and use this in your QMap.

struct GameObjectInterface {
};

class Safe : public GameObjectInterface {};
class Mushroom : public GameObjectInterface {};
class Floor : public GameObjectInterface {};

QMap<int, GameObjectInterface*> _GameObjects;

// Is game object with ID `n` a `Safe`?

Safe* s = dynamic_cast<Safe*>(_GameObjects[n]);
if (s != nullptr) {
    // Yes it is a safe
}

Another possibility:

QList<QMap<int, GameObjectInterface*>> _GameObjects;

And if you want you can capsule everything into one struct as hinted by other responders.

struct MyGameObject {
    QMap<int, Safe*> Safes;
    QMap<int, Mushrooms*> Mushrooms;
    QMap<int, Floor*> Floors;
};

QList<MyGameObject> _GameObjects;

If each are related (same key for all objects) it could be simplified like:

struct MyGameObject {
    Safe* _Safe;
    Mushrooms* _Mushroom;
    Floor* _Floor;
};
QMap<int, MyGameObject*> _GameObjects;
bkausbk
  • 2,740
  • 1
  • 36
  • 52
0

You could keep pointer to base class for all your specific objects:

QMap<int, MyBaseClass*> allObjects;
AnatolyS
  • 4,249
  • 18
  • 28