Results 1 to 15 of 15

Thread: QMap to keep the model statistcs

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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?

  2. #2
    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

  3. #3
    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 

  4. #4
    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 ...

  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

    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...

  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

    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 

  7. 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.