As @André said the solution relying on QMap
keys()
and values()
methods is not efficient at all and shouldn't be used.
Instead you should use iterators!
so using the same .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;
void setMap(QMap<int, QString>* map);
private:
QMap<int, QString>* _map;
};
#endif // MAPMODEL_H
the cpp would be something like this:
#include "mapmodel.h"
MapModel::MapModel(QObject *parent) :
QAbstractTableModel(parent), _map(nullptr)
{}
void MapModel::setMap(QMap<int, QString>* map)
{
beginModelReset();
_map = map;
endModelReset();
}
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 || !index.isValid() || index.row() >= _map->count() || role != Qt::DisplayRole)
return QVariant();
auto it = _map.cbegin();
it += index.row();
if (index.column() == 0)
return it.key();
if (index.column() == 1)
return it.value();
return QVariant();
}
The main change is in MapModel::data
where you use an iterator and as @Claudiu commented, you must use beginModelReset
and endModelReset
in MapModel::setMap
. (to allow you to be able to change the map on your model and signal your Views)
If you want some examples, I've done it for QAbstractListModel
on FamilyModel2
. Here is the header, here the cpp. It is using a wrapper for the Map that you can find here.
Check the constructor FamilyModel2::FamilyModel2
where external signals are connected in order to manage insertion of entries and also emit dataChanged
when your data is updated ;)
beginModelReset()
andendModelReset()
insetMap
– Midwest