1

I got a QMap with an identifier and a corresponding object. When subclassing QAbstractTableModel::data() you get a QModelIndex with row and column, respectively. Each row should represent one object (QAbstractTableModel::rowCount() is myMap->size()).

Is it legit to get the current object via

myMap->values().at(index.row())

Has this implications (sorting, inserting), because the identifiers of the (unsorted) map are by-passed? I mean for QAbstractTableModel::setData() I need to do the same map identifier by-passing?! Thanks.

braggPeaks
  • 1,158
  • 10
  • 23

3 Answers3

1

Quite late answer, but can still be useful:

(myMap->constBegin() + index.row()).key();
(myMap->constBegin() + index.row()).value();

will do the trick avoiding the copy of values() method

Moia
  • 2,216
  • 1
  • 12
  • 34
0

When you say "identifier", I assume you mean key, and "the corresponding object" is the value. QMap is by-definition sorted by key.

If you never intend to use the QMap key-value functionality, you should consider storing your values in a QList container and accessing that based on the row index as you suggested.

Frankie Simon
  • 721
  • 1
  • 7
  • 14
-1

QMap::values returns all values in ascending order of their keys, so probably your code will work. Still, I'd use something like following:

myMap[this->index(index.row(), 0).data().toString()]

provided that you call it from QAbstractTableModel, and your keys are in 0th column.

user2155932
  • 760
  • 3
  • 9