ExpertDev

Untitled

Oct 4th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. CustomItemDelegate::CustomItemDelegate(QObject *parent)
  2. : QStyledItemDelegate (parent)
  3. {
  4.  
  5. }
  6. QWidget* CustomItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
  7. {
  8. if (index.column() != 3)
  9. return nullptr;
  10.  
  11. QLineEdit *editor = new QLineEdit(parent);
  12. connect(editor, &QLineEdit::returnPressed, this, &CustomItemDelegate::commitAndCloseEditor);
  13. return editor;
  14. }
  15. void CustomItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
  16. {
  17. QStyledItemDelegate::paint(painter, option, index);
  18. }
  19. void CustomItemDelegate::commitAndCloseEditor()
  20. {
  21. QLineEdit *editor = qobject_cast<QLineEdit*>(sender());
  22. emit commitData(editor);
  23. emit closeEditor(editor);
  24. }
  25. void CustomItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
  26. {
  27. if (index.column() != 3)
  28. return;
  29.  
  30. QString value = index.model()->data(index, Qt::DisplayRole).toString();
  31.  
  32. QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor);
  33. lineEdit->setText(value);
  34. qDebug() << value;
  35. }
  36. void CustomItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
  37. {
  38. if (index.column() != 3)
  39. return;
  40.  
  41. QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
  42. QString value = lineEdit->text();
  43. model->setData(index, value, Qt::EditRole);
  44. }
  45. void CustomItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
  46. {
  47. editor->setGeometry(option.rect);
  48. }
  49. bool CustomItemDelegate::eventFilter(QObject *object, QEvent *event)
  50. {
  51. return true;
  52. }
Add Comment
Please, Sign In to add comment