ok so i'm trying to get a combobox to show up in my tree view, but for some reason it won't allow me to edit it, my view select thing is set for row select.

Also is there a way i can edit an item while hovering over it, so e.g i hover over an item with a combobox and just click the box and it comes up with options.

Qt Code:
  1. inline void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  2. {
  3. QStyleOptionComboBox comboBoxOption;
  4. comboBoxOption.rect = option.rect;
  5. comboBoxOption.state = QStyle::State_Enabled;
  6. comboBoxOption.frame = true;
  7. comboBoxOption.currentText = index.data().toString();
  8. QApplication::style()->drawComplexControl(QStyle::CC_ComboBox, &comboBoxOption, painter);
  9. //QApplication::style()->drawControl(QStyle::CE_ComboBoxLabel, &comboBoxOption, painter);
  10.  
  11. }
  12.  
  13. inline QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
  14. {
  15. qDebug() << "Editor created";
  16. // ComboBox ony in column 2
  17.  
  18. // Create the combobox and populate it
  19. QComboBox *cb = new QComboBox(parent);
  20. int row = index.row();
  21. cb->addItem(QString("one in row %1").arg(row));
  22. cb->addItem(QString("two in row %1").arg(row));
  23. cb->addItem(QString("three in row %1").arg(row));
  24. return cb;
  25. }
  26.  
  27. inline void setEditorData ( QWidget *editor, const QModelIndex &index ) const
  28. {
  29. if(QComboBox *cb = qobject_cast<QComboBox *>(editor)) {
  30. // get the index of the text in the combobox that matches the current value of the itenm
  31. QString currentText = index.data(Qt::EditRole).toString();
  32. int cbIndex = cb->findText(currentText);
  33. // if it is valid, adjust the combobox
  34. if(cbIndex >= 0)
  35. cb->setCurrentIndex(cbIndex);
  36. } else {
  37. QStyledItemDelegate::setEditorData(editor, index);
  38. }
  39. }
  40. inline void setModelData ( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
  41. {
  42. if(QComboBox *cb = qobject_cast<QComboBox *>(editor))
  43. // save the current text of the combo box as the current value of the item
  44. model->setData(index, cb->currentText(), Qt::EditRole);
  45. else
  46. QStyledItemDelegate::setModelData(editor, model, index);
  47. }
  48. inline void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
  49. {
  50. editor->setGeometry(option.rect);
  51. }
To copy to clipboard, switch view to plain text mode