Results 1 to 15 of 15

Thread: QMap to keep the model statistcs

  1. #1
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QMap to keep the model statistcs

    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMap to keep the model statistcs

    You don't emit proper signals where appropriate. If you change the value in an existing index, you need to emit dataChanged() signal with proper parameters.

    BTW. You have three columns in your model yet you return data only for two of them...

  3. #3
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMap to keep the model statistcs

    Quote Originally Posted by wysota View Post
    You don't emit proper signals where appropriate. If you change the value in an existing index, you need to emit dataChanged() signal with proper parameters.
    AAh....so when I update a count I have to emit a dataCHanged() signal ? the problem is that it takes indexes as the argument...and those I dont know in that method
    Quote Originally Posted by wysota View Post
    BTW. You have three columns in your model yet you return data only for two of them...
    yes I know...the last one will be editable...I wanted to make work first the statistics
    Last edited by gyre; 10th December 2007 at 01:21. Reason: updated contents

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMap to keep the model statistcs

    Quote Originally Posted by gyre View Post
    AAh....so when I update a count I have to emit a dataCHanged() signal ? the problem is that it takes indexes as the argument...and those I dont know in that method
    That's why it's probably better to use a QList instead of QMap and store both the id and the count in the structure kept in the list. You might also use QHash for a fast location of an id within the list. Otherwise you'll need a way to map an id to index and that might be difficult without a container that preserves order of items.

  5. #5
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMap to keep the model statistcs

    Quote Originally Posted by wysota View Post
    That's why it's probably better to use a QList instead of QMap and store both the id and the count in the structure kept in the list. You might also use QHash for a fast location of an id within the list. Otherwise you'll need a way to map an id to index and that might be difficult without a container that preserves order of items.
    But even if I used QList I wouldnt know the number of the indexes....or would I ?
    So you are saying that tehere is now way to make it work this way ?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMap to keep the model statistcs

    Quote Originally Posted by gyre View Post
    But even if I used QList I wouldnt know the number of the indexes....or would I ?
    You would.
    So you are saying that tehere is now way to make it work this way ?
    Yes, of course. For instance like so:

    Qt Code:
    1. QHash<int, int> m_lookupIndex; // maps id to index in the list
    2. struct Item{
    3. int id;
    4. int count;
    5. Item(){id=-1;count=0;}
    6. };
    7. QList<Item> m_data;
    8.  
    9. void Model::insert(...){
    10. int id = getId();
    11. Item item;
    12. item.id = id;
    13. item.count = 1;
    14. int index = m_data.count();
    15. m_list.append(item);
    16. m_lookupIndex[id] = index;
    17. //...
    18. }
    19.  
    20. QModelIndex Model::idToIndex(int id){
    21. if(!m_lookupIndex.contains(id))
    22. return QModelIndex();
    23. int ind = m_lookupIndex[id];
    24. // it'd be 2 times faster using iterators, but it's just an example, so...
    25. return index(ind, 0);
    26. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMap to keep the model statistcs

    Quote Originally Posted by wysota View Post
    You would.

    Yes, of course. For instance like so:

    Qt Code:
    1. QHash<int, int> m_lookupIndex; // maps id to index in the list
    2. struct Item{
    3. int id;
    4. int count;
    5. Item(){id=-1;count=0;}
    6. };
    7. QList<Item> m_data;
    8.  
    9. void Model::insert(...){
    10. int id = getId();
    11. Item item;
    12. item.id = id;
    13. item.count = 1;
    14. int index = m_data.count();
    15. m_list.append(item);
    16. m_lookupIndex[id] = index;
    17. //...
    18. }
    19.  
    20. QModelIndex Model::idToIndex(int id){
    21. if(!m_lookupIndex.contains(id))
    22. return QModelIndex();
    23. int ind = m_lookupIndex[id];
    24. // it'd be 2 times faster using iterators, but it's just an example, so...
    25. return index(ind, 0);
    26. }
    To copy to clipboard, switch view to plain text mode 
    fuh you got me confused I must say ...thanks anyway

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMap to keep the model statistcs

    Quote Originally Posted by gyre View Post
    fuh you got me confused I must say
    Why is that?

  9. #9
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMap to keep the model statistcs

    I have implemented as you suggested it to me...
    But after I compiled...I tried to run the program...and I got a big memory leake of Qt libraries...dunno why ...

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

    header:
    Qt Code:
    1. typedef struct countstats{
    2. int id;
    3. int count;
    4. QString comments;
    5. } CountStats;
    6.  
    7. class MsgStatsModel : public QAbstractTableModel
    8. {
    9. Q_OBJECT
    10.  
    11. typedef QList<CountStats> StatList;
    12. QHash<int, int> indexHash;
    13.  
    14. public:
    15. int rowCount(const QModelIndex &parent = QModelIndex()) const;
    16. int columnCount(const QModelIndex &parent = QModelIndex()) const;
    17. QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    18. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
    19.  
    20. StatList idStatsList;
    21. public slots:
    22. void updateStats(const QVcaCanMsg &canmsg);
    23. public:
    24. MsgStatsModel(QObject *parent = 0);
    25. ~MsgStatsModel();
    26. };
    To copy to clipboard, switch view to plain text mode 

    so no success

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMap to keep the model statistcs

    Memory leak? How do you know you have a memory leak?

    How does the application behave?

  11. #11
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMap to keep the model statistcs

    Quote Originally Posted by wysota View Post
    Memory leak? How do you know you have a memory leak?

    How does the application behave?
    it exits with the following error log...
    *** glibc detected *** /home/gyre/temp/06_filter_widget_added/bin/qcanalyzer: realloc(): invalid old size: 0x080dbf38 ***
    ======= Backtrace: =========
    /lib/libc.so.6[0xb734dbdd]
    /lib/libc.so.6(realloc+0xfe)[0xb734fb4e]
    /usr/lib/libQtCore.so.4(_Z8qReallocPvj+0x24)[0xb7597bf8]
    /usr/lib/libQtCore.so.4(_ZN7QString7reallocEi+0x24c)[0xb75df3b4]
    /usr/lib/libQtCore.so.4(_ZN7QString6appendE5QChar+0x78)[0xb75e099a]
    /usr/lib/libQtCore.so.4[0xb759abba]
    /usr/lib/libQtCore.so.4[0xb7613e14]
    /usr/lib/libQtCore.so.4[0xb7610a3d]
    /usr/lib/libQtCore.so.4[0xb761104c]
    /usr/lib/libQtCore.so.4[0xb7611498]
    /usr/lib/libQtCore.so.4(_ZNK9QResource12isCompressedEv+0x1f )[0xb7611717]
    /usr/lib/libQtCore.so.4[0xb7611929]
    /usr/lib/libQtCore.so.4[0xb7611a4a]
    /usr/lib/libQtCore.so.4(_ZN19QAbstractFileEngine6createERK7 QString+0x5b)[0xb75ea4eb]
    /usr/lib/libQtCore.so.4[0xb75fe9d9]
    /usr/lib/libQtCore.so.4(_ZN9QFileInfoC1ERK7QString+0x4a)[0xb75fed9a]
    /usr/lib/libQtGui.so.4(_ZN5QIconC1ERK7QString+0x2e)[0xb793ba60]
    /home/gyre/temp/06_filter_widget_added/bin/qcanalyzer[0x805ba16]
    /home/gyre/temp/06_filter_widget_added/bin/qcanalyzer[0x8057a63]
    /home/gyre/temp/06_filter_widget_added/bin/qcanalyzer(_ZNK18QAbstractItemModel5matchERK11QMod elIndexiRK8QVarianti6QFlagsIN2Qt9MatchFlagEE+0x4f3 )[0x80535c7]
    /lib/libc.so.6(__libc_start_main+0xe0)[0xb72f8050]
    /home/gyre/temp/06_filter_widget_added/bin/qcanalyzer(_ZNK6QFrame8sizeHintEv+0x8d)[0x8053421]
    ======= Memory map: ========
    08048000-080b2000 r-xp 00000000 08:03 1934881 /home/gyre/temp/06_filter_widget_added/bin/qcanalyzer
    080b2000-080b3000 rw-p 0006a000 08:03 1934881 /home/gyre/temp/0f54000 rw-p 00001000 08:03 3883263 /lib/libdl-2.6.1.so
    b6f54000-b703c000 r-xp 00000000 08:03 5051900 /usr/lib/libX11.so.6.2.0
    b703c000-b7040000 rw-p 000e8000 08:03 5051900 /usr/lib/libX11.so.6.2.0
    b7040000-b704d000 r-xp 00000000 08:03 5049986 /usr/lib/libXext.so.6.4.0
    b704d000-b704e000 rw-p 0000c000 08:03 5049986 /usr/lib/libXext.so.6.4.0
    b704e000-b7071000 r-xp 00000000 08:03 5051365 /usr/lib/libfontconfig.so.1.2.0
    b7071000-b7079000 rw-p 00023000 08:03 5051365 /usr/lib/libfontconfig.so.1.2.0
    b7079000-b707a000 rw-p b7079000 00:00 0
    b707a000-b70e5000 r-xp 00000000 08:03 1032250 /usr/lib/libfreetype.so.6.3.16
    b70e5000-b70e9000 rw-p 0006a000 08:03 1032250 /usr/lib/libfreetype.so.6.3.16
    b70e9000-b70eb000 r-xp 00000000 08:03 1032259 /usr/lib/libXinerama.so.1.0.0
    b70eb000-b70ec000 rw-p 00001000 08:03 1032259 /usr/lib/libXinerama.so.1.0.0
    b70ec000-b70f4000 r-xp 00000000 08:03 1032243 /usr/lib/libXcursor.so.1.0.2
    b70f4000-b70f5000 rw-p 00007000 08:03 1032243 /usr/lib/libXcursor.so.1.0.2
    b70f5000-b70f9000 r-xp 00000000 08:03 5051904 /usr/lib/libXfixes.so.3.1.0
    b70f9000-b70fa000 rw-p 00003000 08:03 5051904 /usr/lib/libXfixes.so.3.1.0
    [stack]

    and so on

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMap to keep the model statistcs

    It's not a memory leak. You must have made a mistake in your code...

    Here is something that works for me:
    Qt Code:
    1. #include <QApplication>
    2. #include <QAbstractListModel>
    3. #include <QListView>
    4. #include <QList>
    5. #include <QHash>
    6. #include <QTimerEvent>
    7.  
    8. class Model : public QAbstractListModel {
    9. public:
    10. Model() : QAbstractListModel() {m_tid = -1;}
    11. void add(int id){
    12. if(m_hash.contains(id)){
    13. int row = m_hash[id];
    14. m_data[row].count++;
    15. emit dataChanged(index(row), index(row));
    16. } else {
    17. int row = m_data.count();
    18. beginInsertRows(QModelIndex(), row, row);
    19. S s;
    20. s.id = id;
    21. s.count = 1;
    22. m_data.append(s);
    23. m_hash[id] = row;
    24. endInsertRows();
    25. }
    26. }
    27. int columnCount(const QModelIndex&) const { return 1; }
    28. int rowCount(const QModelIndex &) const { return m_data.count(); }
    29. QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const{
    30. if(role!=Qt::DisplayRole || !hasIndex(index.row(), index.column(), index.parent()))
    31. return QVariant();
    32. int row = index.row();
    33. return QString("%1: %2").arg(m_data[row].id).arg(m_data[row].count);
    34. }
    35. QModelIndex parent(const QModelIndex &) const { return QModelIndex(); }
    36. void start(){ m_tid = startTimer(100); }
    37. protected:
    38. void timerEvent(QTimerEvent *e){
    39. if(e->timerId()==m_tid){
    40. int id = qrand() % 20;
    41. add(id);
    42. } else
    43. QAbstractListModel::timerEvent(e);
    44. }
    45. private:
    46. struct S {
    47. int id;
    48. int count;
    49. S(){id=-1;count=1;}
    50. };
    51. QList<S> m_data;
    52. QHash<int, int> m_hash;
    53. int m_tid;
    54. };
    55.  
    56. int main(int argc, char **argv){
    57. QApplication app(argc, argv);
    58. Model m;
    59. lv.setModel(&m);
    60. m.start();
    61. lv.show();
    62. return app.exec();
    63. }
    To copy to clipboard, switch view to plain text mode 

  13. #13
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMap to keep the model statistcs

    I went through my code milion times ...
    Thanks buddy...Ill take a look at yours and try to find a mistake ...

  14. #14
    Join Date
    Sep 2007
    Posts
    99
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QMap to keep the model statistcs

    so...I remade my codes...now they compile OK...and my app doesnt exit with the code as above...but the statistics dont get actualized ...even when I emit dataChanged() signal when the data get updated...but they still dont ...
    I slowly but surely give up ...

    Qt Code:
    1. int MsgStatsModel::rowCount(const QModelIndex &parent) const
    2. {
    3. Q_UNUSED(parent);
    4. return idStatsList.count();
    5.  
    6. }
    7.  
    8. int MsgStatsModel::columnCount(const QModelIndex &parent) const
    9. {
    10. Q_UNUSED(parent);
    11. return 2;
    12.  
    13. }
    14.  
    15. QVariant MsgStatsModel::headerData(int section, Qt::Orientation orientation, int role) const
    16. {
    17. QVariant ret = QAbstractTableModel::headerData(section, orientation, role);
    18. if(orientation == Qt::Horizontal){
    19. if(role == Qt::DisplayRole){
    20. switch(section){
    21. case 0 :
    22. return tr("ID");
    23. break;
    24. case 1 :
    25. return tr("Count");
    26. break;
    27. case 2 :
    28. return tr("Comment");
    29. break;
    30. default :
    31. break;
    32. }
    33. }
    34. }
    35. return ret;
    36. }
    37.  
    38. QVariant MsgStatsModel::data(const QModelIndex &index, int role) const
    39. {
    40.  
    41. QVariant ret;
    42.  
    43. if(!index.isValid()){
    44. std::cout <<"Invalid Index of data provided!"<< std::endl;
    45. return ret;
    46. }
    47.  
    48. int row = index.row();
    49. int col = index.column();
    50.  
    51. if(row >= 0 && row < rowCount()) {
    52. if(role == Qt::DisplayRole) {
    53. switch(col) {
    54. case 0 :
    55. ret = idStatsList[row].id;
    56. break;
    57. case 1 :
    58. ret = idStatsList[row].count;
    59. break;
    60. case 2 :
    61. //ret = item.comments;
    62. ret=QVariant();
    63. break;
    64. default :
    65. break;
    66. }
    67. }
    68. }
    69. return ret;
    70. }
    71.  
    72. //SLOT catching new arriving messages
    73. void MsgStatsModel::updateStats(const QVcaCanMsg &canmsg)
    74. {
    75. int id = canmsg.id();
    76.  
    77. if(indexHash.contains(id)){
    78. int row = indexHash[id];
    79. idStatsList[row].count++;
    80. emit dataChanged(index(row, 0), index(row, 0));
    81. } else {
    82. int row = idStatsList.count();
    83. beginInsertRows(QModelIndex(), row, row);
    84. StatsItem item;
    85. item.id = id;
    86. item.count = 1;
    87. idStatsList.append(item);
    88. indexHash[id] = row;
    89. endInsertRows();
    90. }
    91. }
    To copy to clipboard, switch view to plain text mode 

    something is wrong...but cant figure out what...statistics get stored CORRECTLY...count is incremented nicely even for different ids...the problem is display in TableView...

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QMap to keep the model statistcs

    Qt Code:
    1. emit dataChanged(index(row, 0), index(row, 0));
    To copy to clipboard, switch view to plain text mode 
    If you have three columns, I suggest you emit it like:
    Qt Code:
    1. emit dataChanged(index(row, 0), index(row, 2));
    To copy to clipboard, switch view to plain text mode 

    If you emit changes only in the first column, nothing gets updated, because your count is in the second column, so the correct version is:
    Qt Code:
    1. emit dataChanged(index(row, 1), index(row, 1));
    To copy to clipboard, switch view to plain text mode 

  16. The following user says thank you to wysota for this useful post:

    gyre (10th December 2007)

Similar Threads

  1. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  2. Coin3d + Qt: SIGLNALs and SLOTs
    By vonCZ in forum Newbie
    Replies: 26
    Last Post: 15th May 2009, 07:34
  3. QMap model data
    By gyre in forum Newbie
    Replies: 3
    Last Post: 9th December 2007, 22:19
  4. Model Choices review/questions
    By ucntcme in forum Qt Programming
    Replies: 1
    Last Post: 16th September 2007, 21:57
  5. QMap destructor bug
    By Ruud in forum Qt Programming
    Replies: 6
    Last Post: 8th April 2006, 09:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.