-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelegate.cpp
More file actions
34 lines (29 loc) · 1 KB
/
delegate.cpp
File metadata and controls
34 lines (29 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "delegate.h"
Delegate::Delegate(QObject *parent) :
QItemDelegate(parent) {}
QWidget *Delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &) const
{
QLineEdit *editor = new QLineEdit(parent);
connect(editor, &QLineEdit::editingFinished, this, &Delegate::commitAndCloseEditor);
return editor;
}
void Delegate::commitAndCloseEditor()
{
QLineEdit *editor = qobject_cast<QLineEdit *>(sender());
emit commitData(editor);
emit closeEditor(editor);
}
void Delegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
edit->setText(index.model()->data(index, Qt::EditRole).toString());
return;
}
void Delegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
if (edit) {
model->setData(index, edit->text());
return;
}
}