Hi everybody,

I have managed to create a custom model by implementing QAbstractModel and setting my data from a vector of structures(setting the first 3 columns). But when I try and add checkboxes on the fourth column I seem unable to make these checkable or uncheckable.
The last 2 days I have been searching the internet and understand that I need a virtual method which is called setData. Even so I seem unable to get it to work as I want. (The fourth column shows the checkboxes, but I cannot change to checked / unchecked).

Any help or advice towards the correct way to do this would be highly appreciated. Other then the issue with the checkbox the Abstract model is usable in a Qtableview and visualises 3.7 million rows of genome data without a glitch and at high speed. (which impressed me).

header
Qt Code:
  1. #ifndef DATALISTMODEL_H
  2. #define DATALISTMODEL_H
  3.  
  4. #include <QAbstractTableModel>
  5. #include <QMap>
  6. #include <vector>
  7. #include <QFont>
  8. #include <QCheckBox>
  9.  
  10. struct dataStructRow
  11. {
  12. std::string markername;
  13. std::string samplename;
  14. std::string geno;
  15. };
  16.  
  17. class dataListModel : public QAbstractTableModel
  18. {
  19. public:
  20. explicit dataListModel(std::vector<dataStructRow> &dataStructList, QObject *parent = 0);
  21.  
  22. int rowCount(const QModelIndex &parent) const;
  23. int columnCount(const QModelIndex &parent) const;
  24. QVariant data(const QModelIndex &index, int role) const;
  25. QVariant headerData(int section, Qt::Orientation orientation,
  26. int role = Qt::DisplayRole) const;
  27. Qt::ItemFlags flags ( const QModelIndex &index ) const;
  28. bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole);
  29.  
  30. private:
  31. dataStructRow dataMap;
  32. std::vector<dataStructRow>& _dataStructList;
  33. QList<QString> _headers;
  34. QFont _font;
  35. };
  36.  
  37. #endif
To copy to clipboard, switch view to plain text mode 

Implementation
Qt Code:
  1. #include <QtCore>
  2.  
  3. #include "datalistmodel.h"
  4.  
  5. dataListModel::dataListModel(std::vector<dataStructRow> &dataStructList, QObject *parent)
  6. : QAbstractTableModel(parent), _dataStructList(dataStructList), _font(QFont("Courier", 10, QFont::Bold))
  7. {
  8. _headers << "Markername" << "Sample" << "genotype" << "Select";
  9. }
  10.  
  11.  
  12. int dataListModel::rowCount(const QModelIndex & /* parent */) const
  13. {
  14. return _dataStructList.size();
  15. }
  16.  
  17. int dataListModel::columnCount(const QModelIndex & /* parent */) const
  18. {
  19. return 4;
  20. }
  21.  
  22. QVariant dataListModel::data(const QModelIndex &index, int role) const
  23. {
  24. if (!index.isValid()) {
  25. return QVariant();
  26. } else if (role == Qt::DisplayRole) {
  27. dataStructRow perRowdataStruct = _dataStructList[index.row()];
  28. switch(index.column()) {
  29. case 0:
  30. return QString::fromStdString(perRowdataStruct.markername);
  31. case 1:
  32. return QString::fromStdString(perRowdataStruct.samplename);
  33. case 2:
  34. return QString::fromStdString(perRowdataStruct.geno);
  35. default:
  36. return QVariant();
  37. }
  38. } else if (index.column() == 3 && role == Qt::CheckStateRole) {
  39. return Qt::ItemIsUserCheckable;
  40. } else {
  41. return QVariant();
  42. }
  43. }
  44.  
  45. QVariant dataListModel::headerData(int section, Qt::Orientation orientation, int role) const
  46. {
  47. if (role == Qt::DisplayRole) {
  48. if (orientation == Qt::Horizontal){
  49. return _headers[section];
  50. }
  51. } else if ( role == Qt::FontRole) {
  52. return _font;
  53. }
  54. return QVariant();
  55. }
  56.  
  57. bool dataListModel::setData(const QModelIndex & index, const QVariant & value, int role)
  58. {
  59. if (index.isValid() && role == Qt::EditRole) {
  60. emit dataChanged(index, index);
  61. return true;
  62. }
  63. return false;
  64. }
  65.  
  66.  
  67. Qt::ItemFlags dataListModel::flags(const QModelIndex& index) const
  68. {
  69. Qt::ItemFlags flags = QAbstractItemModel::flags(index);
  70. flags |= (Qt::ItemIsSelectable
  71. |Qt::ItemIsUserCheckable
  72. |Qt::ItemIsEnabled);
  73. return flags;
  74. }
To copy to clipboard, switch view to plain text mode