I am working on a project and I need something like a property editor for it.
So I use TreeView for it. I defined my own classes derived from QAbstractItemModel and QStyledItemDelegate. Looks great.

for editing simple values I use QLineEditor`s etc.
But now I need editor for color and here's what I do:
1. I define QPushButton as editor
2. I click on a cell ad my button appears
3. I click on the button, it opens QColorDialog and I select a color.
4. I close color dialog and emit closeEditor and here it crashed! As I understand, when QBushButton(or any else editor) loses focus it removed by TreeView.

Code snippet to show my idea:
Qt Code:
  1. QWidget *PropertyDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
  2. {
  3. PropertyItem * item = (PropertyItem *)(index.internalPointer());
  4. if(item)
  5. {
  6. QWidget * editor;
  7. switch(item->GetPropertyType())
  8. {
  9. ...
  10. case PropertyItem::PropertyTypeColor:
  11. editor = new QPushButton(parent);
  12. ((QPushButton *)editor)->setText("...");
  13. connect(((QPushButton *)editor),SIGNAL(clicked()),this,SLOT(colorTypeClicked()));
  14. return editor;
  15. break;
  16.  
  17. default:
  18. return QStyledItemDelegate::createEditor(parent,option,index);
  19. }
  20. }
  21. return QStyledItemDelegate::createEditor(parent,option,index);
  22. }
  23. void PropertyDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  24. {
  25. PropertyItem * item = static_cast<PropertyItem*>(index.internalPointer());
  26. if(item)
  27. {
  28. switch(item->GetPropertyType())
  29. {
  30. ...
  31. case PropertyItem::PropertyTypeColor:
  32. currentValue = item->GetValue();
  33. break;
  34. default: QStyledItemDelegate::setEditorData(editor,index);
  35. }
  36. }
  37. }
  38. void PropertyDelegate::colorTypeClicked()
  39. {
  40. QWidget * editor = qobject_cast<QWidget *>(sender());
  41. QColor current = currentValue.value<QColor>();
  42. currentValue = QColorDialog::getColor(current,NULL,tr("Select color"));
  43. editor->setFocus();
  44. emit commitData(editor);
  45. emit closeEditor(editor); // crashed here
  46. }
To copy to clipboard, switch view to plain text mode 

So my question - how can I keep it from deleting? Or, if my way looks wrong, any good idea to edit color in property editor.

Thanks for advice!

P.S. I've searched the forum, I've googled it but I've not found some usefull code