Hi,
I have subclassed a QAbstractTableModel to represent data from a QMap. This QMap has QLists of QSqlRecords and this map is modified by some other part of my code. I want to use this model with a QTableView to display the sql records in this map for each key. Here is my code.

Qt Code:
  1. #ifndef MYMODEL_H
  2. #define MYMODEL_H
  3.  
  4. #include <QAbstractTableModel>
  5. #include <QSqlRecord>
  6. #include <QList>
  7.  
  8.  
  9. class MyModel : public QAbstractTableModel
  10. {
  11. Q_OBJECT
  12.  
  13. public:
  14. MyModel(QObject *parent = 0);
  15. int rowCount(const QModelIndex &parent = QModelIndex()) const;
  16. int columnCount(const QModelIndex &parent = QModelIndex()) const;
  17. QVariant data(const QModelIndex &index, int role) const;
  18. void setRecordMap(QMap<int, QList<QSqlRecord>> *map);
  19. void setSelectedSerMsgIndex(QModelIndex *index);
  20.  
  21. private:
  22. QMap<int, QList<QSqlRecord>> *recordMap;
  23. QModelIndex *selectedSerendibMsgIndex;
  24. };
  25.  
  26. #endif
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. #include "mymodel.h"
  2.  
  3.  
  4. MyModel::MyModel(QObject *parent) : QAbstractTableModel(parent)
  5. {
  6.  
  7. }
  8.  
  9. int MyModel::rowCount(const QModelIndex &parent) const
  10. {
  11. if(recordMap->isEmpty())
  12. return 0;
  13.  
  14. int row = selectedSerendibMsgIndex->row();
  15. return recordMap->value(row).size();
  16. }
  17.  
  18. int MyModel::columnCount(const QModelIndex &parent) const
  19. {
  20. if(recordMap->isEmpty())
  21. return 0;
  22.  
  23. int row = selectedSerendibMsgIndex->row();
  24. return recordMap->value(row).at(0).count();
  25. }
  26.  
  27. QVariant MyModel::data(const QModelIndex &index, int role) const
  28. {
  29. if(recordMap->isEmpty())
  30. return QVariant();
  31.  
  32. if (!index.isValid())
  33. return QVariant();
  34.  
  35. int row = selectedSerendibMsgIndex->row();
  36. if (index.row() >= recordMap->value(row).size())
  37. return QVariant();
  38.  
  39. if (role == Qt::DisplayRole)
  40. {
  41. return recordMap->value(row).value(index.row()).value(index.column()); /* QVariant("hello");*/
  42. }
  43. else
  44. {
  45. return QVariant();
  46. }
  47. }
  48.  
  49. void MyModel::setRecordMap(QMap<int, QList<QSqlRecord>> *map)
  50. {
  51. recordMap = map;
  52. }
  53.  
  54. void MyModel::setSelectedSerMsgIndex(QModelIndex *index)
  55. {
  56. selectedSerendibMsgIndex = index;
  57. }
To copy to clipboard, switch view to plain text mode 

But the problem is, I cannot see the data from the map. I am guessing it is because there's something wrong with my implementation of the data() method. But I can't figure out what it is. Please be kind enough to help me. Thank you.