Hi I managed to implement storing of "message" statistics...
I made a QMap <id, count> where I store tha stats for particular id
The only problem is that although they get updated upon new arriving messages they dont change actually displayed value...
I will explain:
I get 10 messages in a row of id=4.
But in view there is only displayed count=1 despite the fact that count contains 10.
The correct value is displayed when a message with the different id arrives...
Does anyone know what is wrong with that ?

My model:
Qt Code:
  1. class MsgStatsModel : public QAbstractTableModel
  2. {
  3. Q_OBJECT
  4.  
  5. typedef QMap<int, int> IdStatsMap;
  6.  
  7.  
  8. public:
  9. int rowCount(const QModelIndex &parent = QModelIndex()) const;
  10. int columnCount(const QModelIndex &parent = QModelIndex()) const;
  11. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
  12. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
  13.  
  14. IdStatsMap idStats;
  15. public slots:
  16. void updateStats(const QVcaCanMsg &canmsg);
  17. public:
  18. MsgStatsModel(QObject *parent = 0);
  19. ~MsgStatsModel();
  20. };
  21.  
  22. MsgStatsModel::MsgStatsModel(QObject *parent)
  23. {
  24. // std::cout <<"Model Created!"<<"\n";
  25. }
  26.  
  27. MsgStatsModel::~MsgStatsModel()
  28. {
  29.  
  30. // std::cout <<"Model Destroyed!"<<"\n";
  31.  
  32. }
  33.  
  34. int MsgStatsModel::rowCount(const QModelIndex &parent) const
  35. {
  36. Q_UNUSED(parent);
  37. return idStats.size();
  38.  
  39. }
  40.  
  41. int MsgStatsModel::columnCount(const QModelIndex &parent) const
  42. {
  43. Q_UNUSED(parent);
  44. return 3;
  45.  
  46. }
  47.  
  48. QVariant MsgStatsModel::headerData(int section, Qt::Orientation orientation, int role) const
  49. {
  50. QVariant ret = QAbstractTableModel::headerData(section, orientation, role);
  51. if(orientation == Qt::Horizontal){
  52. if(role == Qt::DisplayRole){
  53. switch(section){
  54. case 0 :
  55. return tr("ID");
  56. break;
  57. case 1:
  58. return tr("Count");
  59. break;
  60. case 2 :
  61. return tr("Comment");
  62. break;
  63. default :
  64. break;
  65. }
  66. }
  67. }
  68. return ret;
  69. }
  70.  
  71. QVariant MsgStatsModel::data(const QModelIndex &index, int role) const
  72. {
  73.  
  74. QVariant ret;
  75.  
  76. if(!index.isValid()){
  77. std::cout <<"Invalid Index of data provided!"<< std::endl;
  78. return ret;
  79. }
  80. QList<int> keys = idStats.keys();
  81.  
  82. int row = index.row();
  83. int col = index.column();
  84.  
  85. if(row >= 0 && row < rowCount()) {
  86. if(role == Qt::DisplayRole) {
  87. int key = keys[row];
  88. int count = idStats.value(key);
  89. switch(col) {
  90. case 0 :
  91. ret = key;
  92. break;
  93. case 1:
  94. ret = count;
  95. break;
  96. }
  97. }
  98. }
  99. return ret;
  100. }
  101.  
  102. void MsgStatsModel::updateStats(const QVcaCanMsg &canmsg)
  103. {
  104. qDebug()<< "WE GET UPDATE!";
  105. int key = canmsg.id();
  106. int old_row_count = idStats.size();
  107. if(idStats.contains(key)){
  108. int count = idStats.value(key);
  109. qDebug() <<"KEY IS: " << key<<"COUNT IS: " <<count;
  110. idStats[key] = count+1;
  111. } else {
  112. beginInsertRows(QModelIndex(), old_row_count, old_row_count);
  113. idStats.insert(key, 1);
  114. endInsertRows();
  115. }
  116. }
To copy to clipboard, switch view to plain text mode