I have ComboBox with checkable items made with CheckBox delegate. When this ComboBox is opened the first item's delegate's createEditor() is not called but when you move to second item it is called and delegate is properly created. After this when you move back to first item then delegate is working. The problem is only with selecting first item for first time. If you have got only one item in comboBox this item is unselectable.

This is my delegate's code:
Qt Code:
  1. #include "checkboxlistdelegate.h"
  2.  
  3. CheckBoxListDelegate::CheckBoxListDelegate(QObject *parent)
  4. : QItemDelegate(parent) {
  5. }
  6.  
  7. void CheckBoxListDelegate::paint(QPainter *painter,
  8. const QStyleOptionViewItem &option,
  9. const QModelIndex &index) const {
  10. //Get item data
  11. bool value = index.data(Qt::UserRole).toBool();
  12. QString text = index.data(Qt::DisplayRole).toString();
  13. // fill style options with item data
  14. const QStyle *style = QApplication::style();
  15. opt.state |= value ? QStyle::State_On : QStyle::State_Off;
  16. opt.state |= QStyle::State_Enabled;
  17. opt.text = text;
  18. opt.rect = option.rect;
  19. opt.palette = QPalette(Qt::white);
  20. // draw item data as CheckBox
  21. style->drawControl(QStyle::CE_CheckBox,&opt,painter);
  22. }
  23.  
  24. QWidget* CheckBoxListDelegate::createEditor(QWidget *parent,
  25. const QStyleOptionViewItem& option,
  26. const QModelIndex & index ) const {
  27. QCheckBox *editor = new QCheckBox(parent);
  28. editor->setStyleSheet("QCheckBox {background-color: #aaaaaa; color: white;}");
  29. return editor;
  30. }
  31.  
  32. void CheckBoxListDelegate::setEditorData(QWidget *editor,
  33. const QModelIndex &index) const {
  34. QCheckBox *myEditor = static_cast<QCheckBox*>(editor);
  35. myEditor->setText(index.data(Qt::DisplayRole).toString());
  36. myEditor->setChecked(index.data(Qt::UserRole).toBool());
  37. }
  38.  
  39. void CheckBoxListDelegate::setModelData(QWidget *editor,
  40. const QModelIndex &index) const {
  41. //get the value from the editor (CheckBox)
  42. QCheckBox *myEditor = static_cast<QCheckBox*>(editor);
  43. bool value = myEditor->isChecked();
  44. //set model data
  45. QMap<int,QVariant> data;
  46. data.insert(Qt::DisplayRole,myEditor->text());
  47. data.insert(Qt::UserRole,value);
  48. model->setItemData(index,data);
  49. emit indexChanged(index);
  50. }
  51.  
  52. void CheckBoxListDelegate::updateEditorGeometry(QWidget *editor,
  53. const QStyleOptionViewItem &option,
  54. const QModelIndex &index ) const {
  55. Q_UNUSED(index);
  56. editor->setGeometry(option.rect);
To copy to clipboard, switch view to plain text mode 

And this is code of my QComboBox subclass:

Qt Code:
  1. CheckBoxList::CheckBoxList(QWidget *widget)
  2. : QComboBox(widget),title(""),selection() {
  3. // set delegate items view
  4. view()->setItemDelegate(new CheckBoxListDelegate(this));
  5. view()->setStyleSheet("QAbstractItemView {background-color:white;}");
  6. // Enable editing on items view
  7. view()->setEditTriggers(QAbstractItemView::AllEditTriggers);
  8. connect(view()->model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)),
  9. this, SLOT(onItemClicked(QModelIndex)));
  10. }
  11.  
  12. void CheckBoxList::paintEvent(QPaintEvent *) {
  13. QStylePainter painter(this);
  14. painter.setPen(palette().color(QPalette::Text));
  15. initStyleOption(&opt);
  16. opt.currentText = title;
  17. painter.drawComplexControl(QStyle::CC_ComboBox, opt);
  18. painter.drawControl(QStyle::CE_ComboBoxLabel, opt);
  19. }
  20.  
  21. void CheckBoxList::setDisplayText(QString text) {
  22. title = text;
  23. }
  24.  
  25. QString CheckBoxList::getDisplayText() const {
  26. return title;
  27. }
  28.  
  29. bool CheckBoxList::isChecked(const QModelIndex& index) const {
  30. return view()->model()->itemData(index).value(Qt::UserRole).toBool();
  31. }
To copy to clipboard, switch view to plain text mode 

Thank You!