Results 1 to 4 of 4

Thread: QAbstractTableModel subclass data() method implementation

  1. #1
    Join Date
    Mar 2011
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Red face QAbstractTableModel subclass data() method implementation

    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.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QAbstractTableModel subclass data() method implementation

    Have you single-stepped through data() with your debugger? Have you checked that your rowCount() implementation is correct and not returning 0 when you are no expecting it?

  3. #3
    Join Date
    Mar 2011
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QAbstractTableModel subclass data() method implementation

    Thanks Chris for the quick reply. I haven't used the debugger on data() method yet, and I sure will do that. But about the rowCount() implementation, I'm sure that at some point it returns something other than 0 because I am adding stuff to my QMap in another part of my code. So recordMap->isEmpty() will be false and recordMap->value(row).size() will be returned.

  4. #4
    Join Date
    Mar 2011
    Posts
    9
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QAbstractTableModel subclass data() method implementation

    I used the debugger on data() method, but strangely, it's never even called. Is there a particular reason for this?

Similar Threads

  1. reset method in QAbstractTableModel
    By TorAn in forum Qt Programming
    Replies: 1
    Last Post: 2nd August 2010, 05:06
  2. GraphicsView paintEvent() method implementation problem
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 2
    Last Post: 17th August 2009, 06:54
  3. Replies: 1
    Last Post: 20th May 2009, 20:36
  4. QAbstractTableModel , custom data
    By akon in forum Newbie
    Replies: 0
    Last Post: 17th April 2009, 16:03
  5. QAbstractTableModel::data not being called...
    By steg90 in forum Qt Programming
    Replies: 1
    Last Post: 5th June 2007, 09:52

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.