I have a need for a combobox that has a list of items that are check-able.

I found similar code on another site but it has not provided a complete solution.

I have constructed a working edit ComboBox delegate and the delegate appears as expected listing the items. However, I am unable to set or unset the check attribute by clicking on the item ... or any other keyboard or mouse action for that matter.

The one difference in my delegate and the other examples is that I am using QVector<QStandardItems*> items - instead of std::vector.

I have noticed that if I add the selection attribute, I can select the item and the dropbox closes and the desired result occurs for that item only. That is not the action I desire. I wish to check all appropriate items on the list then close the dropdown and evaluate the list.

Here is my unfinished code for discussion. I know there are better ways to do this but I have to start somewhere.

The code here does present a combobox in the appropriate column / cell of a table view with the items listed properly. However, the items do not present as QCheckbox items -- merely text item. The also do not allow a selection of any kind. I have missed something somewhere and I am not sure what.

I have looked at the other posts on similar issues but it has not revealed anything obvious to me.

editdaysdelegate.hpp
Qt Code:
  1. #include <QtCore/QSize>
  2. #include <QtCore/QModelIndex>
  3. #include <QtGui/QStandardItemModel>
  4. #include <QtWidgets/QApplication>
  5. #include <QtWidgets/QStylePainter>
  6. #include <QtWidgets/QWidget>
  7. #include <QtWidgets/QAbstractItemView>
  8. #include <QtWidgets/QStyleOptionViewItem>
  9. #include <QtWidgets/QItemDelegate>
  10. #include <QtWidgets/QComboBox>
  11. #include <QtWidgets/QCheckBox>
  12.  
  13. #define Sun_On 1;
  14. #define Mon_On 2;
  15. #define Tue_On 4;
  16. #define Wed_On 8;
  17. #define Thu_On 16;
  18. #define Fri_On 32;
  19. #define Sat_On 64;
  20.  
  21. #define Sun_Off 63;
  22. #define Mon_Off 95;
  23. #define Tue_Off 111;
  24. #define Wed_Off 119;
  25. #define Thu_Off 123;
  26. #define Fri_Off 125;
  27. #define Sat_Off 126;
  28.  
  29. class CheckBoxListDelegate : public QItemDelegate
  30. {
  31. Q_OBJECT
  32.  
  33. public:
  34. CheckBoxListDelegate(QObject *parent);
  35. virtual ~CheckBoxListDelegate();
  36.  
  37. QWidget *createEditor( QWidget *parent,
  38. const QStyleOptionViewItem &option,
  39. const QModelIndex &index
  40. ) const Q_DECL_OVERRIDE;
  41.  
  42. void setEditorData(QWidget *editor,const QModelIndex &index) const Q_DECL_OVERRIDE;
  43. void setModelData(QWidget *editor,QAbstractItemModel *model,const QModelIndex &index) const Q_DECL_OVERRIDE;
  44. void setCurrentValue(const quint8 val);
  45. private slots:
  46. void slot_changed(const QModelIndex& topLeft, const QModelIndex& bottomRight);
  47.  
  48. private:
  49. quint8 m_value; // the current bit matrix value from the table
  50.  
  51. QVector<QStandardItem *> items;
  52.  
  53. QComboBox *combo;
  54.  
  55.  
  56. };
To copy to clipboard, switch view to plain text mode 

editdaysdelegate.cpp
Qt Code:
  1. #include <QtCore/QDebug>
  2. #include <QtCore/QVector>
  3. #include <QtWidgets/QStyle>
  4. #include <QtWidgets/QStyleOption>
  5. #include <QtGui/QStandardItem>
  6. #include <QtGui/QStandardItemModel>
  7. #include <QtGui/QPainter>
  8.  
  9. #include "editdaysdelegate.hpp"
  10.  
  11. //==== Delegate implementation ====//
  12. CheckBoxListDelegate::CheckBoxListDelegate(QObject *parent) : QItemDelegate(parent)
  13. {
  14. m_value = 0;
  15.  
  16. combo = new QComboBox; // the widget to be delegated
  17.  
  18. model = new QStandardItemModel; // model for the delegate
  19.  
  20. item1 = new QStandardItem;
  21. item1->setText("Sun");
  22. item1->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
  23. item1->setData(Qt::Unchecked, Qt::CheckStateRole);
  24. model->insertRow(0, item1);
  25. items.append(item1);
  26.  
  27. item2 = new QStandardItem;
  28. item2->setText("Mon");
  29. item2->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
  30. item2->setData(Qt::Unchecked, Qt::CheckStateRole);
  31. model->insertRow(1, item2);
  32. items.append(item2);
  33.  
  34. item3 = new QStandardItem;
  35. item3->setText("Tue");
  36. item3->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
  37. item3->setData(Qt::Unchecked, Qt::CheckStateRole);
  38. model->insertRow(2, item3);
  39. items.append(item3);
  40.  
  41. item4 = new QStandardItem;
  42. item4->setText("Wed");
  43. item4->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
  44. item4->setData(Qt::Unchecked, Qt::CheckStateRole);
  45. model->insertRow(3, item4);
  46. items.append(item4);
  47.  
  48. item5 = new QStandardItem;
  49. item5->setText("Thu");
  50. item5->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
  51. item5->setData(Qt::Unchecked, Qt::CheckStateRole);
  52. model->insertRow(4, item5);
  53. items.append(item5);
  54.  
  55. item6 = new QStandardItem;
  56. item6->setText("Fri");
  57. item6->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
  58. item6->setData(Qt::Unchecked, Qt::CheckStateRole);
  59. model->insertRow(5, item6);
  60. items.append(item6);
  61.  
  62. item7 = new QStandardItem;
  63. item7->setText("Sat");
  64. item7->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
  65. item7->setData(Qt::Unchecked, Qt::CheckStateRole);
  66. model->insertRow(6, item7);
  67. items.append(item7);
  68.  
  69. connect(model, SIGNAL(dataChanged(QModelIndex,QModelIndex,QVector<int>)), this, SLOT(slot_changed(const QModelIndex &, const QModelIndex &)));
  70.  
  71. combo->setModel(model);
  72. }
  73.  
  74. CheckBoxListDelegate::~CheckBoxListDelegate()
  75. {
  76. delete item1;
  77. delete item2;
  78. delete item3;
  79. delete item4;
  80. delete item5;
  81. delete item6;
  82. delete item7;
  83. delete model;
  84. }
  85.  
  86. void CheckBoxListDelegate::setCurrentValue(const quint8 val)
  87. {
  88. m_value = val;
  89. }
  90.  
  91. void CheckBoxListDelegate::slot_changed(const QModelIndex &topLeft, const QModelIndex &bottomRight)
  92. {
  93. Q_UNUSED(bottomRight)
  94. quint16 row;
  95.  
  96. row = topLeft.row();
  97. QStandardItem *item = items[row];
  98. if ( item->checkState() == Qt::Unchecked )
  99. {
  100. switch ( item->row() )
  101. {
  102. case 0:
  103. m_value = m_value & Sun_Off;
  104. break;
  105. case 1:
  106. m_value = m_value & Mon_Off;
  107. break;
  108. case 2:
  109. m_value = m_value & Tue_Off;
  110. break;
  111. case 3:
  112. m_value = m_value & Wed_Off;
  113. break;
  114. case 4:
  115. m_value = m_value & Thu_Off;
  116. break;
  117. case 5:
  118. m_value = m_value & Fri_Off;
  119. break;
  120. case 6:
  121. m_value = m_value & Sat_Off;
  122. break;
  123. }
  124. }
  125. else if ( item->checkState() == Qt::Checked )
  126. {
  127. switch ( item->row() )
  128. {
  129. case 0:
  130. m_value = m_value |= Sun_On;
  131. break;
  132. case 1:
  133. m_value = m_value |= Mon_On;
  134. break;
  135. case 2:
  136. m_value = m_value |= Tue_On;
  137. break;
  138. case 3:
  139. m_value = m_value |= Wed_On;
  140. break;
  141. case 4:
  142. m_value = m_value |= Thu_On;
  143. break;
  144. case 5:
  145. m_value = m_value |= Fri_On;
  146. break;
  147. case 6:
  148. m_value = m_value |= Sat_On;
  149. break;
  150. }
  151. }
  152. }
  153.  
  154. QWidget* CheckBoxListDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
  155. {
  156. Q_UNUSED(option)
  157. Q_UNUSED(index)
  158.  
  159. // create check box as our editor
  160. QComboBox *editor = new QComboBox(parent);
  161. editor->setModel(model);
  162. return editor;
  163. }
  164.  
  165. void CheckBoxListDelegate::setEditorData(QWidget *editor,const QModelIndex &index) const
  166. {
  167. //set editor data
  168. QComboBox myEditor = static_cast<QComboBox>(editor);
  169. myEditor->itemDelegate()->setModelData(editor,model,index);
  170. }
  171.  
  172. void CheckBoxListDelegate::setModelData(QWidget *editor,QAbstractItemModel *model,const QModelIndex &index) const
  173. {
  174. //get the value from the editor (CheckBox)
  175. QCheckBox myEditor = static_cast<QCheckBox>(editor);
  176. bool value = myEditor->isChecked();
  177.  
  178. //set model data
  179. QMap<int,QVariant> data;
  180. data.insert(Qt::DisplayRole,myEditor->text());
  181. data.insert(Qt::UserRole,value);
  182. model->setItemData(index,data);
  183. }
To copy to clipboard, switch view to plain text mode