Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CustomItemDelegate::CustomItemDelegate(QObject *parent)
- : QStyledItemDelegate (parent)
- {
- }
- QWidget* CustomItemDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option, const QModelIndex& index) const
- {
- if (index.column() != 3)
- return nullptr;
- QLineEdit *editor = new QLineEdit(parent);
- connect(editor, &QLineEdit::returnPressed, this, &CustomItemDelegate::commitAndCloseEditor);
- return editor;
- }
- void CustomItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
- {
- QStyledItemDelegate::paint(painter, option, index);
- }
- void CustomItemDelegate::commitAndCloseEditor()
- {
- QLineEdit *editor = qobject_cast<QLineEdit*>(sender());
- emit commitData(editor);
- emit closeEditor(editor);
- }
- void CustomItemDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
- {
- if (index.column() != 3)
- return;
- QString value = index.model()->data(index, Qt::DisplayRole).toString();
- QLineEdit *lineEdit = qobject_cast<QLineEdit*>(editor);
- lineEdit->setText(value);
- qDebug() << value;
- }
- void CustomItemDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
- {
- if (index.column() != 3)
- return;
- QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
- QString value = lineEdit->text();
- model->setData(index, value, Qt::EditRole);
- }
- void CustomItemDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
- {
- editor->setGeometry(option.rect);
- }
- bool CustomItemDelegate::eventFilter(QObject *object, QEvent *event)
- {
- return true;
- }
Add Comment
Please, Sign In to add comment