Hello.
I bumped into a problem of adding an item QCheckBox into QComboBox. What I`ve found is the code for creating ItemDelegate for QComboBox, but it makes all of items in QComboBox as QCheckBox. My task is to add just one check box into the list of common combobox`s items...
Here is a code I found:
Qt Code:
  1. class CheckBoxList: public QComboBox
  2. {
  3. Q_OBJECT;
  4.  
  5. public:
  6. CheckBoxList(QWidget *widget = 0);
  7. virtual ~CheckBoxList();
  8. virtual void paintEvent(QPaintEvent *);
  9. void SetDisplayText(QString text);
  10. QString GetDisplayText() const;
  11.  
  12. private:
  13. QString m_DisplayText;
  14. };
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. class CheckBoxListDelegate : public QItemDelegate
  2. {
  3. public:
  4. CheckBoxListDelegate(QObject *parent)
  5. : QItemDelegate(parent)
  6. {
  7. ;
  8. }
  9.  
  10. void paint(QPainter *painter, const QStyleOptionViewItem &option,
  11. const QModelIndex &index) const
  12. {
  13. //Get item data
  14. bool value = index.data(Qt::UserRole).toBool();
  15. QString text = index.data(Qt::DisplayRole).toString();
  16.  
  17. // fill style options with item data
  18. const QStyle *style = QApplication::style();
  19. opt.state |= value ? QStyle::State_On : QStyle::State_Off;
  20. opt.state |= QStyle::State_Enabled;
  21. opt.text = text;
  22. opt.rect = option.rect;
  23.  
  24. // draw item data as CheckBox
  25. style->drawControl(QStyle::CE_CheckBox,&opt,painter);
  26. }
  27.  
  28. QWidget *createEditor(QWidget *parent,
  29. const QStyleOptionViewItem & option ,
  30. const QModelIndex & index ) const
  31. {
  32. // create check box as our editor
  33. QCheckBox *editor = new QCheckBox(parent);
  34. return editor;
  35. }
  36.  
  37. void setEditorData(QWidget *editor,
  38. const QModelIndex &index) const
  39. {
  40. //set editor data
  41. QCheckBox *myEditor = static_cast<QCheckBox*>(editor);
  42. myEditor->setText(index.data(Qt::DisplayRole).toString());
  43. myEditor->setChecked(index.data(Qt::UserRole).toBool());
  44. }
  45.  
  46. void setModelData(QWidget *editor, QAbstractItemModel *model,
  47. const QModelIndex &index) const
  48. {
  49. //get the value from the editor (CheckBox)
  50. QCheckBox *myEditor = static_cast<QCheckBox*>(editor);
  51. bool value = myEditor->isChecked();
  52.  
  53. //set model data
  54. QMap<int,QVariant> data;
  55. data.insert(Qt::DisplayRole,myEditor->text());
  56. data.insert(Qt::UserRole,value);
  57. model->setItemData(index,data);
  58. }
  59. };
  60.  
  61.  
  62.  
  63. CheckBoxList::CheckBoxList(QWidget *widget )
  64. :QComboBox(widget),m_DisplayText("")
  65. {
  66. // set delegate items view
  67. view()->setItemDelegate(new CheckBoxListDelegate(this));
  68.  
  69. // Enable editing on items view
  70. view()->setEditTriggers(QAbstractItemView::CurrentChanged);
  71. }
  72.  
  73.  
  74. CheckBoxList::~CheckBoxList()
  75. {
  76. ;
  77. }
  78.  
  79.  
  80. void CheckBoxList::paintEvent(QPaintEvent *)
  81. {
  82. QStylePainter painter(this);
  83. painter.setPen(palette().color(QPalette::Text));
  84.  
  85. // draw the combobox frame, focusrect and selected etc.
  86. initStyleOption(&opt);
  87.  
  88. // if no display text been set , use "..." as default
  89. if(m_DisplayText.isNull())
  90. opt.currentText = "...";
  91. else
  92. opt.currentText = m_DisplayText;
  93. painter.drawComplexControl(QStyle::CC_ComboBox, opt);
  94.  
  95. // draw the icon and text
  96. painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
  97. }
  98.  
  99.  
  100. void CheckBoxList::SetDisplayText(QString text)
  101. {
  102. m_DisplayText = text;
  103. }
  104.  
  105. QString CheckBoxList::GetDisplayText() const
  106. {
  107. return m_DisplayText;
  108. }
To copy to clipboard, switch view to plain text mode 

Is anyone have thoughts how to improve this code to get possibility for adding into QComboBox 2 types of items: QCheckBox and common QComboBox items?
Or there is some other ways to achieve such behaviour?