1

I am working on a project that has a 2d map:

QMap < int, QMap < int, QPixmap> > slot_pic;

I set up a table to initialize the values:

slot_pic[0][0] = QPixmap("path...");
slot_pic[1][0] = QPixmap("path...");
slot_pic[2][0] = QPixmap("path...");
slot_pic[3][0] = QPixmap("path...");
...

Occasionally, the second key would get changed like this:

...
slot_pic[300][0] = QPixmap("path...");
slot_pic[300][1] = QPixmap("path...");
slot_pic[300][2] = QPixmap("path...");
slot_pic[300][3] = QPixmap("path...");
slot_pic[301][0] = QPixmap("path...");
...

At a certain point in my program, I come across a key that I only want to look at the first number. The path of the pixmap is then only dependent on the first key. Like this:

...
slot_pic[357][0] = QPixmap("path...");
slot_pic[358]    = QPixmap("path...");
slot_pic[359][0] = QPixmap("path...");
...

But obviously, I get a syntax error. How can I tell my program to only look at the first key if that key is a number I define? (358) I COULD write it a thousand times each time like this:

...
slot_pic[358][1] = QPixmap("path...");
slot_pic[358][2] = QPixmap("path...");
...
slot_pic[358][50] = QPixmap("path...");
slot_pic[358][55] = QPixmap("path...");
...

So basically, how can I make a 2d map only dependent on one key?

NOTE: I do not want to change the value of the second key, it is needed later in my program. I only want to stop looking at it at a certain key in my map.

I tried to explain this as best as I could. Any help would be appreciated :)

mrg95
  • 2,371
  • 11
  • 46
  • 89

1 Answers1

2

Method 1

Make a class that has an underlying container of a 2D vector or a 2D map, but has all the accessor methods that you want.

C++ multidimensional array operator

Operator[][] overload

Then you can throw in an if statement in the middle of one of your accessors methods and have it return an empty or default value instead of the stored values, if you turn it on that way.

Method 2

Instead of storing a pixmap in your map, store a struct that has information such as if it is turned on or off, in addition to the pixmap.

struct MyPixmap
{
    bool enabled;
    QPixmap pixmap;
}

Method 3

Have the outer layer of your map point at a struct or class that has a default argument system in place, in addition to the ability to pointing at the map.

Hope that helps. Let me know if you need more info or a better example of one of these.

EDIT: Here is some code for getting Method 3 to work. It uses templates. Hopefully it makes sense. You probably should implement a copy constructor for MyMap, and a destructor... but it probably will work without it.

http://www.cplusplus.com/doc/tutorial/templates/

main.cpp

#include <QApplication>
#include "mymap.h"
#include <QString>
#include <QDebug>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QMap < int, MyMap < int, QString> > slot_pic;

    slot_pic[0][0] = "0,0";
    slot_pic[1][0] = "1,0";
    slot_pic[2][0] = "2,0";
    slot_pic[3][0] = "3,0";

    slot_pic[300][0] = "300,0";
    slot_pic[300][1] = "300,1";
    slot_pic[300][2] = "300,2";
    slot_pic[300][3] = "300,3";
    slot_pic[301][0] = "301,0";

    slot_pic[357][0] = "357,0";
    slot_pic[358][0] = "358,0";
    slot_pic[359][0] = "359,0";

//    slot_pic[358].default_value() = slot_pic[0][0];

//    slot_pic[357][1] = slot_pic[358].default_value();

    qDebug() << slot_pic;

    qDebug() << slot_pic[358].default_value();

    qDebug() << slot_pic[300].default_value();

    return a.exec();
}

mymap.h

#ifndef MYMAP_H
#define MYMAP_H

#include <QMap>

template <class Key, class T>
class MyMap : public QMap <Key, T>
{
public:
    MyMap();

    T & operator[] ( const Key & key );

    T & default_value ();
private:
    Key m_default_key;
};

// Since template classes are generated
// on demand by the compiler, the implementation should
// stay in this class.

template <typename Key, typename T>
MyMap <Key, T>::MyMap(): QMap<Key, T>()
{

}

template <class Key, class T>
T & MyMap <Key, T>::operator[] ( const Key & key )
{
    // Store the key and use it as a default

    m_default_key = key;

    return QMap::operator [](key);
}

template <class Key, class T>
T & MyMap <Key, T>::default_value ()
{
    return QMap::operator [](m_default_key);
}

#endif // MYMAP_H
Community
  • 1
  • 1
phyatt
  • 18,472
  • 5
  • 61
  • 80
  • Lol your answer seems well thought out and planned, yet im embarrassed to say that I am still quite a noob when it comes to c++. I don't know what a vector, struct, or class is, does, or how to use them. :/ It's late but I will mess with it tomorrow and get back to you :) Thanks – mrg95 Jul 15 '13 at 03:30