Results 1 to 2 of 2

Thread: QtAbstractModel and checkboxes

  1. #1
    Join Date
    Oct 2012
    Posts
    1
    Qt products
    Qt3 Qt4 Qt Jambi
    Platforms
    Unix/X11 Windows

    Default QtAbstractModel and checkboxes

    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 

  2. #2
    Join Date
    Feb 2013
    Location
    San Diego
    Posts
    37
    Thanks
    14
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QtAbstractModel and checkboxes

    I think the problem is how you reimplemented data() and setData().
    Take a look at that thread below. It explains how to do it with a QFileSyetemModel but it should be easily applicable to your case.
    http://www.qtcentre.org/threads/2725...ith-checkboxes

Similar Threads

  1. Many checkboxes and one slot
    By mirelon in forum Qt Programming
    Replies: 5
    Last Post: 17th May 2013, 07:42
  2. QFileSystemModel with checkboxes...
    By been_1990 in forum Qt Programming
    Replies: 14
    Last Post: 11th March 2011, 15:01
  3. qtreeview + checkboxes
    By lamera in forum Newbie
    Replies: 9
    Last Post: 6th September 2008, 22:10
  4. checkboxes in a tableview,
    By mimmo_kallon in forum Newbie
    Replies: 3
    Last Post: 13th March 2008, 19:01
  5. checkboxes
    By abrou in forum Newbie
    Replies: 2
    Last Post: 1st February 2008, 18:52

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.