While using my custom QItemDelegate, the QLineEdit editor is not always closing. The closeEditor() signal is emitted but the QLineEdit object remains in the QTableWidgetItem.

I am using 4.2.2 and setting the edit triggers using QAbstractItemView::AllEditTriggers.

This happens when I have multiple rows in the table and I click (speed is irrelevant) on the editable items:
  • Click on row 0, column 1
  • Click on row 1, column 1
  • Click on row 2, column 1
  • repeat
Eventually one of the editors in column 1 will get stuck on, even though the closeEditor() signal was emitted. The qDebug() statement shows the contents of the item where the editor should be closed and is displayed even when the editor does not go away. This state does not allow any other editors to appear in the table, effectively stopping the user from editing any other items.

Has anyone else seen this type of behavior? Any ideas how to prevent this?

Qt Code:
  1. class DMXTableCellDelegate : public QItemDelegate
  2. {
  3. Q_OBJECT
  4.  
  5. public:
  6. DMXTableCellDelegate( QObject* parent = 0 ) : QItemDelegate( parent )
  7. { m_parent = parent; }
  8.  
  9. QWidget* createEditor( QWidget* parent, const QStyleOptionViewItem& option,
  10. const QModelIndex& index ) const;
  11.  
  12. private slots:
  13. void commitAndCloseEditor( void );
  14.  
  15. private:
  16. QObject* m_parent;
  17.  
  18. };
  19.  
  20. QWidget* DMXTableCellDelegate::createEditor( QWidget* parent,
  21. const QStyleOptionViewItem& option,
  22. const QModelIndex& index ) const
  23. {
  24. if ( index.column() == COL_Name )
  25. {
  26. QLineEdit* lineEdit = new QLineEdit( parent );
  27. lineEdit->setMaxLength( DEVICE_NAME_MAX );
  28.  
  29. connect( lineEdit, SIGNAL( returnPressed() ),
  30. this, SLOT( commitAndCloseEditor() ) );
  31. connect( lineEdit, SIGNAL( editingFinished() ),
  32. this, SLOT( commitAndCloseEditor() ) );
  33.  
  34. return lineEdit;
  35. }
  36. else
  37. return QItemDelegate::createEditor( parent, option, index );
  38. }
  39.  
  40. void DMXTableCellDelegate::commitAndCloseEditor( void )
  41. {
  42. QLineEdit* editor = qobject_cast<QLineEdit*>( sender() );
  43. emit commitData( editor );
  44. qDebug( "emit closeEditor(): %s", editor->text().toAscii().data() );
  45. emit closeEditor( editor, QAbstractItemDelegate::EditNextItem );
  46. }
To copy to clipboard, switch view to plain text mode