Results 1 to 11 of 11

Thread: QCheckBox Item In QDropdown

  1. #1
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QCheckBox Item In QDropdown

    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 

  2. #2
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QCheckBox Item In QDropdown

    I Implemented much complex one long back , I am pasting simplified version of that.
    Sorry If i miss some thing in the code because I copy paste my old code here.


    Below snippet is for : will show set of checkable items in Combo box & only checked items data will be shown in editor with ; separated


    //Checkable combo box
    Qt Code:
    1. class checkStateComboBox : public QComboBox
    2. {
    3. Q_OBJECT
    4.  
    5. protected:
    6. QVector<QStandardItem*> Items;
    7.  
    8. public:
    9. checkStateComboBox( QStringList l_items = QStringList(), QWidget * parent = 0 ) : QComboBox(parent)
    10. {
    11. Model = new QStandardItemModel;
    12. setModel(Model);
    13.  
    14. for(int i=0; i<l_items.count() ;i++)
    15. {
    16. Item->setText(l_items.at(i));
    17. Item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    18. Item->setData(Qt::Checked, Qt::CheckStateRole);
    19. Model->insertRow(Model->rowCount(), Item);
    20. Items.push_back(Item);
    21. }
    22. connect(Model, SIGNAL( itemChanged(QStandardItem*) ), this, SLOT(itemChanged(QStandardItem*) ) );
    23. }
    24. ~checkStateComboBox(){}
    25.  
    26. void addItem(QString &text, Qt::CheckState checkState = Qt::Unchecked)
    27. {
    28. Item->setText(text);
    29. Item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    30. Item->setData(checkState, Qt::CheckStateRole);
    31. Model->insertRow(Model->rowCount(), Item);
    32. Items.push_back(Item);
    33. }
    34.  
    35. void addItems(QStringList& list, Qt::CheckState checkState = Qt::Unchecked)
    36. {
    37. foreach (QString itemData, list)
    38. {
    39. addItem(itemData, checkState);
    40. }
    41. }
    42.  
    43. const QVector<QStandardItem*>& getItems() const
    44. {
    45. return Items;
    46. }
    47.  
    48. const QStandardItem* getItem(const int& index) const
    49. {
    50. if(Items.size() <= index){
    51. return NULL;
    52. }
    53.  
    54. return Items.at(index);
    55. }
    56.  
    57. public slots:
    58. void itemChanged(QStandardItem* /*item*/)
    59. {
    60. //qDebug() << "Item checked.....";
    61. }
    62. };
    To copy to clipboard, switch view to plain text mode 




    //Delegate implementation
    Qt Code:
    1. propertyItemDelegate::propertyItemDelegate(QObject *parent): QStyledItemDelegate(parent)
    2. {
    3. }
    4.  
    5. QWidget *propertyItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex & index ) const
    6. {
    7. QWidget* l_editor = new checkStateComboBox();
    8. l_editor->setParent(parent);
    9.  
    10. return l_editor;
    11. }
    12.  
    13. void propertyItemDelegate::setEditorData(QWidget *f_widget, const QModelIndex &index) const
    14. {
    15. QStringList l_itemsData = //get the list you want to show;
    16. QStringList l_modelData = index.model()->data(index, Qt::DisplayRole).toString().split(";"); //existing data, I am showing only checked items data in editor
    17.  
    18.  
    19. //this logic is to check items which are previously checked based on data I am showing in editor
    20. //I get the data which I am showing in editor & I know those are the only items I checked
    21. checkStateComboBox* l_multiValuedWidget = static_cast<checkStateComboBox*>(f_widget);
    22. foreach(QString l_itemData, l_itemsData)
    23. {
    24. bool l_checkState = l_modelData.contains(l_itemData);
    25. Qt::CheckState l_state = l_checkState ? Qt::Checked : Qt::Unchecked;
    26. l_multiValuedWidget->addItem(l_itemData, l_state);
    27. }
    28. }
    29.  
    30. void propertyItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    31. {
    32. QString l_data = QString::null;
    33.  
    34. checkStateComboBox* l_multiValuedWidget = static_cast<checkStateComboBox*>(f_widget);
    35. int size = l_multiValuedWidget->getItems().count();
    36.  
    37. for(int i=0; i < size; i++)
    38. {
    39. const QStandardItem* l_item = l_multiValuedWidget->getItem(i);
    40. //data of checked items only
    41. if(l_item && (Qt::Checked == l_item->checkState()))
    42. {
    43. QString l_itemData = l_item->data(Qt::DisplayRole).toString();
    44. ((size-1) == i) ? (l_data = l_data + l_itemData) : (l_data = l_data + l_itemData + ";");
    45. }
    46. }
    47.  
    48. model->setData(index, l_data, Qt::EditRole);
    49. }
    50.  
    51. void propertyItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & /*index*/ ) const
    52. {
    53. QRect l_rect = option.rect;
    54. l_rect.adjust(0,0,0,l_rect.height()*0.3);
    55. editor->setGeometry(l_rect);
    56. }
    To copy to clipboard, switch view to plain text mode 
    Thanks :-)

  3. #3
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QCheckBox Item In QDropdown

    prasad_N

    Thanks for your reply.

    Well your code does little different than mine. After creating it for use in my project I got a checkbox at the top of the form instead of the column / cell of the displayed table view being edited and the items are displayed twice and do not appear as checkboxes -- just text items that are not selectable -- no checkboxes at all.


    Maybe I did not indicate before, I am using Qt 5.5.1

  4. #4
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QCheckBox Item In QDropdown

    Can I see some code please ?



    Quote Originally Posted by ad5xj View Post
    After creating it for use in my project I got a checkbox at the top of the form instead of the column / cell of the displayed table view being edited and the items are displayed twice and do not appear as checkboxes -- just text items that are not selectable -- no checkboxes at all.
    I got a checkbox at the top of the form - What do you mean form (view ?). ??
    the items are displayed twice and do not appear as checkboxes - If you see only check box then how you are able to see items (we can see items in combo box only) ?
    Last edited by prasad_N; 4th November 2015 at 12:00.
    Thanks :-)

  5. #5
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QCheckBox Item In QDropdown

    Ok, Perhaps my reply was a bit vague.

    The QMainWindow is the form I referred to; although, I would also like to use this delegate in a QDialog as well. So I guess you could apply either term to the reference of "form" in my reply. I also re-formated your code to fit my project and programming style. I very well may have missed something in the interpretation.

    The resulting QComboBox does not contain any checkbox items - only text items -- none selectable -- nothing happens when I click on any item in the drop-down list. This has been the problem all along. With the original code I was able to display a combobox but none of the items were checkboxes nor selectable. The only difference I am finding is that your code is not placing the delegate in the column / cell selected for edit.

    I am using the delegate in a QTableView item in the UI of either form. It is assigned in the normal way using:

    In the QMainWindow or QDialog:
    Qt Code:
    1. // connect edit delegates
    2. CheckListDelegate* daysdelegate = new CheckListDelegate(this);
    3. ui->tblView->setItemDelegateForColumn(6, daysdelegate); // days
    To copy to clipboard, switch view to plain text mode 

    Here is the code as I have it now. Hope this helps.


    checkstatecombobox.hpp
    Qt Code:
    1. #include <QtWidgets/QWidget>
    2. #include <QtCore/QStringList>
    3. #include <QtWidgets/QComboBox>
    4.  
    5.  
    6. class CheckStateComboBox : public QComboBox
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. CheckStateComboBox(QStringList l_items, QWidget *parent=0);
    12. ~CheckStateComboBox();
    13.  
    14. void addItem(QString &text, Qt::CheckState checkState = Qt::Unchecked);
    15. void addItems(QStringList& list, Qt::CheckState checkState = Qt::Unchecked);
    16.  
    17. const QVector<QStandardItem*>& getItems() const { return Items; }
    18. const QStandardItem* getItem(const int& index) const { if (Items.size() <= index) return NULL; return Items.at(index); }
    19.  
    20. public slots:
    21. void itemChanged(QStandardItem*);
    22.  
    23. protected:
    24. QVector<QStandardItem*> Items;
    25. };
    To copy to clipboard, switch view to plain text mode 

    checkstatecombobox.cpp
    Qt Code:
    1. #include <QtCore/QObject>
    2. #include <QtCore/QString>
    3. #include <QtGui/QStandardItem>
    4. #include <QtGui/QStandardItemModel>
    5.  
    6. #include "checkstatecombobox.hpp"
    7.  
    8. CheckStateComboBox::CheckStateComboBox(QStringList l_items, QWidget *parent) : QComboBox(parent)
    9. {
    10. model = new QStandardItemModel;
    11. setModel(model);
    12.  
    13. for ( int i = 0; i < l_items.count() ; ++i )
    14. {
    15. Item->setText(l_items.at(i));
    16. Item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    17. Item->setData(Qt::Checked, Qt::CheckStateRole);
    18. model->insertRow(model->rowCount(), Item);
    19. Items.push_back(Item);
    20. }
    21. connect(model, SIGNAL(itemChanged(QStandardItem*)), this, SLOT(itemChanged(QStandardItem*)));
    22. }
    23.  
    24. CheckStateComboBox::~CheckStateComboBox()
    25. {
    26. delete model;
    27. }
    28.  
    29. void CheckStateComboBox::addItem(QString &text, Qt::CheckState checkState)
    30. {
    31. Item->setText(text);
    32. Item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    33. Item->setData(checkState, Qt::CheckStateRole);
    34. model->insertRow(model->rowCount(), Item);
    35. Items.push_back(Item);
    36. }
    37.  
    38. void CheckStateComboBox::addItems(QStringList& list, Qt::CheckState checkState)
    39. {
    40. foreach (QString itemData, list)
    41. {
    42. addItem(itemData, checkState);
    43. }
    44. }
    45.  
    46. void CheckStateComboBox::itemChanged(QStandardItem* i)
    47. {
    48. Q_UNUSED(i)
    49. //qDebug() << "Item checked.....";
    50. }
    To copy to clipboard, switch view to plain text mode 


    checklistdelegate.hpp
    Qt Code:
    1. #include <QtCore/QObject>
    2. #include <QtWidgets/QWidget>
    3. #include <QtGui/QStandardItem>
    4. #include <QtWidgets/QStyledItemDelegate>
    5. #include <QtWidgets/QStyleOptionViewItem>
    6.  
    7.  
    8. class CheckStateComboBox;
    9.  
    10. class CheckListDelegate : public QStyledItemDelegate
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. CheckListDelegate(QWidget *parent=0);
    16. ~CheckListDelegate();
    17.  
    18.  
    19. void setEditorData(QWidget *f_widget, const QModelIndex &index) const;
    20. void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    21. void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const;
    22. QWidget createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    23.  
    24. private:
    25. QStringList l_itemsData;
    26. QWidget *l_editor;
    27. };
    To copy to clipboard, switch view to plain text mode 

    checklistdelegate.cpp
    Qt Code:
    1. #include <QtCore/QModelIndex>
    2. #include <QtWidgets/QWidget>
    3. #include <QtWidgets/QStyledItemDelegate>
    4.  
    5. #include "checkstatecombobox.hpp"
    6. #include "checklistdelegate.hpp"
    7.  
    8. CheckListDelegate::CheckListDelegate(QWidget *parent) : QStyledItemDelegate(parent)
    9. {
    10. //
    11. }
    12. CheckListDelegate::~CheckListDelegate() { }
    13.  
    14. QWidget CheckListDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index ) const
    15. {
    16. Q_UNUSED(parent)
    17. Q_UNUSED(option)
    18. Q_UNUSED(index)
    19.  
    20. l_itemsData << "Sun" << "Mon" << "Tue" << "Wed" << "Thu" << "Fri" << "Sat";
    21. l_editor = new CheckStateComboBox(l_itemsData,parent);
    22. return l_editor;
    23. }
    24.  
    25. void CheckListDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &i) const
    26. {
    27. Q_UNUSED(i)
    28. QRect l_rect = option.rect;
    29. l_rect.adjust(0,0,0,l_rect.height()*0.3);
    30. editor->setGeometry(l_rect);
    31. }
    32.  
    33. void CheckListDelegate::setEditorData(QWidget *f_widget, const QModelIndex &index) const
    34. {
    35. QStringList l_modelData = index.model()->data(index, Qt::DisplayRole).toString().split(";"); //existing data, I am showing only checked items data in editor
    36.  
    37. //this logic is to check items which are previously checked based on data I am showing in editor
    38. //I get the data which I am showing in editor & I know those are the only items I checked
    39. CheckStateComboBox* l_multiValuedWidget = static_cast<CheckStateComboBox*>(f_widget);
    40. foreach ( QString l_itemData, l_itemsData )
    41. {
    42. bool l_checkState = l_modelData.contains(l_itemData);
    43. Qt::CheckState l_state = l_checkState ? Qt::Checked : Qt::Unchecked;
    44. l_multiValuedWidget->addItem(l_itemData, l_state);
    45. }
    46. }
    47.  
    48. void CheckListDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    49. {
    50. quint16 i = 0;
    51. QString l_data = QString::null;
    52. const QStandardItem* l_item;
    53.  
    54. CheckStateComboBox* l_multiValuedWidget = static_cast<CheckStateComboBox*>(editor);
    55. int size = l_multiValuedWidget->getItems().count();
    56.  
    57. for ( i = 0; i < size; ++i )
    58. {
    59. l_item = l_multiValuedWidget->getItem(i);
    60. //data of checked items only
    61. if ( l_item && (Qt::Checked == l_item->checkState()) )
    62. {
    63. QString l_itemData = l_item->data(Qt::DisplayRole).toString();
    64. ((size-1) == i) ? (l_data = l_data + l_itemData) : (l_data = l_data + l_itemData + ";");
    65. }
    66. }
    67.  
    68. model->setData(index, l_data, Qt::EditRole);
    69. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QCheckBox Item In QDropdown

    I did quick experiment on Qt 5.4.0, I just edited spin box delegate example, It worked well. here is the code, you just use this as stand alone implementation. If it works fine then It should work in your project too.

    use this code as it is & let me know if you are still unable to see check boxes & can't check/uncheck them.


    //main.cpp

    Qt Code:
    1. #include <QApplication>
    2. #include <QHeaderView>
    3. #include <QStandardItemModel>
    4. #include <QTableView>
    5. #include "delegate.h"
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication app(argc, argv);
    10.  
    11. QStandardItemModel model(4, 2);
    12. QTableView tableView;
    13. tableView.setModel(&model);
    14.  
    15. propertyItemDelegate delegate;
    16. tableView.setItemDelegate(&delegate);
    17.  
    18. tableView.horizontalHeader()->setStretchLastSection(true);
    19.  
    20. for (int row = 0; row < 4; ++row) {
    21. for (int column = 0; column < 2; ++column) {
    22. QModelIndex index = model.index(row, column, QModelIndex());
    23. model.setData(index, QVariant((row + 1) * (column + 1)));
    24. }
    25.  
    26. tableView.setWindowTitle(QObject::tr("Spin Box Delegate"));
    27. tableView.show();
    28. return app.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 


    //delegate.h
    Qt Code:
    1. #ifndef DELEGATE_H
    2. #define DELEGATE_H
    3.  
    4. #include <QComboBox>
    5. #include <QStandardItemModel>
    6. #include <QStyledItemDelegate>
    7. #include <QVector>
    8. #include <QStringList>
    9.  
    10.  
    11. class checkStateComboBox : public QComboBox
    12. {
    13. Q_OBJECT
    14.  
    15. protected:
    16. QVector<QStandardItem*> Items;
    17.  
    18. public:
    19. checkStateComboBox( QStringList l_items = QStringList(), QWidget * parent = 0 ) : QComboBox(parent)
    20. {
    21. Model = new QStandardItemModel;
    22. setModel(Model);
    23.  
    24. for(int i=0; i<l_items.count() ;i++)
    25. {
    26. Item->setText(l_items.at(i));
    27. Item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    28. Item->setData(Qt::Checked, Qt::CheckStateRole);
    29. Model->insertRow(Model->rowCount(), Item);
    30. Items.push_back(Item);
    31. }
    32. connect(Model, SIGNAL( itemChanged(QStandardItem*) ), this, SLOT(itemChanged(QStandardItem*) ) );
    33. }
    34. ~checkStateComboBox(){}
    35.  
    36. void addItem(QString &text, Qt::CheckState checkState = Qt::Unchecked)
    37. {
    38. Item->setText(text);
    39. Item->setFlags(Qt::ItemIsUserCheckable | Qt::ItemIsEnabled);
    40. Item->setData(checkState, Qt::CheckStateRole);
    41. Model->insertRow(Model->rowCount(), Item);
    42. Items.push_back(Item);
    43. }
    44.  
    45. void addItems(QStringList& list, Qt::CheckState checkState = Qt::Unchecked)
    46. {
    47. foreach (QString itemData, list)
    48. {
    49. addItem(itemData, checkState);
    50. }
    51. }
    52.  
    53. const QVector<QStandardItem*>& getItems() const
    54. {
    55. return Items;
    56. }
    57.  
    58. const QStandardItem* getItem(const int& index) const
    59. {
    60. if(Items.size() <= index){
    61. return NULL;
    62. }
    63.  
    64. return Items.at(index);
    65. }
    66.  
    67. public slots:
    68. void itemChanged(QStandardItem* /*item*/)
    69. {
    70. //qDebug() << "Item checked.....";
    71. }
    72. };
    73.  
    74.  
    75. //Delegate class
    76. class propertyItemDelegate : public QStyledItemDelegate
    77. {
    78. Q_OBJECT
    79.  
    80. public:
    81. propertyItemDelegate(QObject *parent=0);
    82.  
    83. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
    84. const QModelIndex &index) const Q_DECL_OVERRIDE;
    85.  
    86. void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;
    87. void setModelData(QWidget *editor, QAbstractItemModel *model,
    88. const QModelIndex &index) const Q_DECL_OVERRIDE;
    89.  
    90. void updateEditorGeometry(QWidget *editor,
    91. const QStyleOptionViewItem &option, const QModelIndex &index) const Q_DECL_OVERRIDE;
    92.  
    93.  
    94. QStringList m_testList;
    95. };
    96.  
    97. #endif
    To copy to clipboard, switch view to plain text mode 



    //delegate.cpp

    Qt Code:
    1. #include "delegate.h"
    2.  
    3. propertyItemDelegate::propertyItemDelegate(QObject *parent): QStyledItemDelegate(parent)
    4. {
    5. m_testList << "Sunday" << "Monday" << "tuesday" << "wednesday" << "friday" << "satday";
    6. }
    7.  
    8. QWidget *propertyItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem& option, const QModelIndex & index ) const
    9. {
    10. QWidget* l_editor = new checkStateComboBox();
    11. l_editor->setParent(parent);
    12.  
    13. return l_editor;
    14. }
    15.  
    16. void propertyItemDelegate::setEditorData(QWidget *f_widget, const QModelIndex &index) const
    17. {
    18. QStringList l_itemsData = m_testList; //get the list you want to show;
    19. QStringList l_modelData = index.model()->data(index, Qt::DisplayRole).toString().split(";"); //existing data, I am showing only checked items data in editor
    20.  
    21.  
    22. //this logic is to check items which are previously checked based on data I am showing in editor
    23. //I get the data which I am showing in editor & I know those are the only items I checked
    24. checkStateComboBox* l_multiValuedWidget = static_cast<checkStateComboBox*>(f_widget);
    25. foreach(QString l_itemData, l_itemsData)
    26. {
    27. bool l_checkState = l_modelData.contains(l_itemData);
    28. Qt::CheckState l_state = l_checkState ? Qt::Checked : Qt::Unchecked;
    29. l_multiValuedWidget->addItem(l_itemData, l_state);
    30. }
    31. }
    32.  
    33. void propertyItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    34. {
    35. QString l_data = QString::null;
    36.  
    37. checkStateComboBox* l_multiValuedWidget = static_cast<checkStateComboBox*>(editor);
    38. int size = l_multiValuedWidget->getItems().count();
    39.  
    40. for(int i=0; i < size; i++)
    41. {
    42. const QStandardItem* l_item = l_multiValuedWidget->getItem(i);
    43. //data of checked items only
    44. if(l_item && (Qt::Checked == l_item->checkState()))
    45. {
    46. QString l_itemData = l_item->data(Qt::DisplayRole).toString();
    47. ((size-1) == i) ? (l_data = l_data + l_itemData) : (l_data = l_data + l_itemData + ";");
    48. }
    49. }
    50.  
    51. model->setData(index, l_data, Qt::EditRole);
    52. }
    53.  
    54. void propertyItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex & /*index*/ ) const
    55. {
    56. QRect l_rect = option.rect;
    57. l_rect.adjust(0,0,0,l_rect.height()*0.3);
    58. editor->setGeometry(l_rect);
    59. }
    To copy to clipboard, switch view to plain text mode 
    Thanks :-)

  7. #7
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QCheckBox Item In QDropdown

    I built a project exactly as you have it and the results are the same as my custom project.

    The QTableView allows the QComboBox to be displayed with all items added -- however, the items are text items NOT CHECKBOX items. No item can be selected from the list.

  8. #8
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QCheckBox Item In QDropdown

    sorry, not sure what is the issue. you can check my attachment of result for the same code.


    delegate.png
    Thanks :-)

  9. #9
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QCheckBox Item In QDropdown

    I do not doubt you. All I am saying is that; I get the window and the table as shown. But, the items you show as check box items are not so for me.

    I am not sure if this is unique to my installation or a bug in Qt 5.5.1.

  10. #10
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QCheckBox Item In QDropdown

    Even I am not sure, The quick check you can do is, Just try to build same code on another version (It worked for me on Qt4.8.6 & 5.4.0) It works fine with these versions then there is an issue with your version.
    One more thing you can try is use QItemDelegate instead of QStyledItemDelegate (Of course there is no much diff b/w them apart from QStyledItemDelegate uses current style to draw items, But it may get solved if there is a bug in QStyledItemDelegate).
    Thanks :-)

  11. #11
    Join Date
    Jun 2007
    Location
    Louisiana
    Posts
    77
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QCheckBox Item In QDropdown

    prasad_N

    Thanks for the replay. I did compile with QItemDelegate instead of the QStyledItemDelegate and no change in result.

    I am beginning to believe there is some kind of bug in the Item Delegate classes. I will try to run your test on Windows and see if it exists there as well. If so, I will post a bug report.


    UPDATE:

    I moved the code to my Windows XP SP3 platform and it runs as advertised. This tends to point to my Linux Mint installation. I am going to try a re-install of Qt 5.5 and see if that makes a difference. I have checked the dependencies and all are installed on my system so if the re-install produces the same result, I will have to post a bug report.
    Last edited by ad5xj; 5th November 2015 at 16:34.

Similar Threads

  1. QCheckBox
    By sajis997 in forum Qt Programming
    Replies: 1
    Last Post: 2nd May 2012, 15:15
  2. QCheckBox as item of QComboBox
    By Grinchman in forum Newbie
    Replies: 6
    Last Post: 23rd June 2011, 14:44
  3. About QCheckBox
    By liushuo0826 in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 16th April 2010, 07:14
  4. QCheckBox value
    By Koas in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2009, 14:33
  5. QCheckbox
    By sonuani in forum Qt Programming
    Replies: 1
    Last Post: 19th February 2008, 14:12

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.