-1

I need to store some data of table type like a QTableWidget but without a GUI. Something along the line of the following code:

QMap<QString, QString, int, QString, int>

Is there a way of achieve this in Qt? My Qt version is 5.3.

BaCaRoZzo
  • 7,502
  • 6
  • 51
  • 82
  • I think this is more of a "Is there a way of doing it in C++" question than directly referring to Qt. Also, you should probably state your desired effect for this data structure. I'm guessing it is "like a map, but with more data". Meaning you still want to have the key sorting and logarithmic key lookup performance of maps. – spellmansamnesty Apr 15 '15 at 17:39
  • 2
    You could acheive this using `QVariant`. http://doc.qt.io/qt-5/qvariant.html. Just map like `QMap` and you can store an entire list for each key. – rhodysurf Apr 15 '15 at 17:42

1 Answers1

1

You seem to be unclear on a few concepts.

A map (also known in some languages as a dictionary) is an associative array. It associates a key to a value, that's about it, there are no "fields" involved whatsoever, just a key and a value.

There is no data type in Qt to model a database table. For such tasks you usually directly use SQL, Qt supports SQL with various different database drivers.

If you don't want to use a database but instead want to have "native" C++ types, you can simply create an object with all the desired fields:

struct Entry {
    QString s1, s2, s3;
    int i1, i2;
};

And then put that into whatever container you want.

QList<Entry> entryList;
QVector<Entry> entryVec;
QSet<Entry> entrySet;

You can wrap the container in a QAbstractListModel, implement the key functions and roles and have that model be used for a table widget or a QML view.

dtech
  • 47,916
  • 17
  • 112
  • 190