Wrap QMapin a subclass QAbstractTableModeland set it to view. The following is a basic functional example:
File "mapmodel.h"
#ifndef MAPMODEL_H
#define MAPMODEL_H
#include <QAbstractTableModel>
#include <QMap>
class MapModel : public QAbstractTableModel
{
Q_OBJECT
public:
enum MapRoles {
KeyRole = Qt::UserRole + 1,
ValueRole
};
explicit MapModel(QObject *parent = 0);
int rowCount(const QModelIndex& parent = QModelIndex()) const;
int columnCount(const QModelIndex& parent = QModelIndex()) const;
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
inline void setMap(QMap<int, QString>* map) { _map = map; }
private:
QMap<int, QString>* _map;
};
#endif
File "mapmodel.cpp"
MapModel::MapModel(QObject *parent) :
QAbstractTableModel(parent)
{
_map = NULL;
}
int MapModel::rowCount(const QModelIndex& parent) const
{
if (_map)
return _map->count();
return 0;
}
int MapModel::columnCount(const QModelIndex & parent) const
{
return 2;
}
QVariant MapModel::data(const QModelIndex& index, int role) const
{
if (!_map)
return QVariant();
if (index.row() < 0 ||
index.row() >= _map->count() ||
role != Qt::DisplayRole) {
return QVariant();
}
if (index.column() == 0)
return _map->keys().at(index.row());
if (index.column() == 1)
return _map->values().at(index.row());
return QVariant();
}
Usage example:
QMap<int, QString> map;
map.insert(1, "value 1");
map.insert(2, "value 2");
map.insert(3, "value 3");
MapModel mapmodel;
mapmodel.setMap(&map);
YourTableView.setModel(&mapmodel);
It will display a table view populated as follows:

source
share