Hello,

I am trying to use an editor made of two QDoubleSpinBox when subclassing QStyledItemDelegate. I have no problem connecting the delegate and editor to the model but I failed to have my editor to show up in my view. I had no such problem with a simpler editor (e.g. a single QSpinBox).

Constructor for the Editor
Qt Code:
  1. Data_editor::Data_editor( QWidget *parent)
  2. :QFrame(parent)
  3. {
  4. lower_bound_spin_ = new QDoubleSpinBox(this);
  5. upper_bound_spin_ = new QDoubleSpinBox(this);
  6.  
  7. QHBoxLayout* layout = new QHBoxLayout(this);
  8. layout->addWidget(new QLabel("Lower", this));
  9. layout->addWidget(lower_bound_spin_);
  10. layout->addStretch();
  11. layout->addWidget(new QLabel("Upper", this));
  12. layout->addWidget(upper_bound_spin_);
  13.  
  14. this->setLayout(layout);
  15.  
  16. this->setFocusPolicy( Qt::StrongFocus );
  17. }
To copy to clipboard, switch view to plain text mode 

Implementation of the delegate
Qt Code:
  1. QWidget *Data_delegate::createEditor(QWidget *parent,
  2. const QStyleOptionViewItem &option,
  3. const QModelIndex &index) const
  4.  
  5. {
  6. Data_editor* editor = new Data_editor( parent);
  7.  
  8. return editor;
  9.  
  10. }
  11.  
  12. void Data_delegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
  13. const QModelIndex &index) const
  14. {
  15. QStyledItemDelegate::paint(painter, option, index);
  16. }
  17.  
  18.  
  19. QSize Data_delegate::sizeHint(const QStyleOptionViewItem &option,
  20. const QModelIndex &index) const
  21. {
  22. return QStyledItemDelegate::sizeHint(option, index);
  23.  
  24. }
  25.  
  26. void Data_delegate::updateEditorGeometry(QWidget *editor,
  27. const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
  28. {
  29. editor->setGeometry(option.rect);
  30. }
To copy to clipboard, switch view to plain text mode 

I played a bit with eventFilter and try to record the log of the events passing through
Qt Code:
  1. bool Data_delegate::eventFilter(QObject *object, QEvent *event)
  2. {
  3. Data_editor *editor = qobject_cast<Data_editor*>(object);
  4. if (!editor)
  5. return false;
  6.  
  7. QWidget *w_focused = QApplication::focusWidget();
  8. QEvent::Type t = event->type();
  9.  
  10. std::string class_name = (w_focused!=0)?w_focused->metaObject()->className():std::string("000");
  11. ostream_<<class_name <<" "<< QString("%1").arg(t).toStdString()+"\n";
  12.  
  13. if (event->type() == QEvent::KeyPress) {
  14. switch (static_cast<QKeyEvent *>(event)->key()) {
  15. case Qt::Key_Tab:
  16. emit commitData(editor);
  17. emit closeEditor(editor, QAbstractItemDelegate::EditNextItem);
  18. return true;
  19. case Qt::Key_Backtab:
  20. emit commitData(editor);
  21. emit closeEditor(editor, QAbstractItemDelegate::EditPreviousItem);
  22. return true;
  23. case Qt::Key_Enter:
  24. case Qt::Key_Return:
  25.  
  26. QMetaObject::invokeMethod(this, "_q_commitDataAndCloseEditor",
  27. Qt::QueuedConnection, Q_ARG(QWidget*, editor));
  28. return false;
  29. case Qt::Key_Escape:
  30. // don't commit data
  31. emit closeEditor(editor, QAbstractItemDelegate::RevertModelCache);
  32. break;
  33. default:
  34. return false;
  35. }
  36. if (editor->parentWidget())
  37. editor->parentWidget()->setFocus();
  38. return true;
  39. } else if (event->type() == QEvent::FocusOut || (event->type() == QEvent::Hide && editor->isWindow())) {
  40. //the Hide event will take care of he editors that are in fact complete dialogs
  41. //if (!editor->isActiveWindow() || (QApplication::focusWidget() != editor)) {
  42. if (!editor->isActiveWindow() || (w_focused != editor)) {
  43. QWidget *w = w_focused;
  44. //QWidget *w = QApplication::focusWidget();
  45. while (w) { // don't worry about focus changes internally in the editor
  46. if (w == editor)
  47. return false;
  48. w = w->parentWidget();
  49. }
  50. emit commitData(editor);
  51. emit closeEditor(editor, NoHint);
  52. }
  53. } else if (event->type() == QEvent::ShortcutOverride) {
  54. if (static_cast<QKeyEvent*>(event)->key() == Qt::Key_Escape) {
  55. event->accept();
  56. return true;
  57. }
  58. }
  59. return false;
  60. }
To copy to clipboard, switch view to plain text mode 

Which output (first column widget className, second Qcolumn Event type):
Data_tree_view 75
Data_tree_view 70
Data_tree_view 70
Data_tree_view 70
Data_tree_view 70
Data_tree_view 69
Data_tree_view 69
Data_tree_view 69
Data_tree_view 69
Data_tree_view 13
Data_tree_view 14
Data_tree_view 17
Data_tree_view 26
Data_editor 8
Data_editor 67
Data_editor 74
Data_editor 12
Data_editor 10
Data_editor 11
Data_editor 12
Data_tree_view 9

The editor is painted (event 12) but is never shown. The last event (which probably occurred when I moved the cursor) is the FocusOut event which commit the data and close the editor.

So what is missing in my delegate/editor such the editor will be shown in my view? Online comments seem to point out to a focus issue but I cannot get it right.

Thanks