-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtabledelegate.cpp
More file actions
83 lines (75 loc) · 2.47 KB
/
tabledelegate.cpp
File metadata and controls
83 lines (75 loc) · 2.47 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include "tabledelegate.h"
#include <QLineEdit>
#include <QDebug>
#include "comboboxlineedit.h"
TableDelegate::TableDelegate(QObject *parent) : QStyledItemDelegate(parent)
{
}
// 创建编辑器
QWidget *TableDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &,
const QModelIndex & index) const
{
int nDataType = index.model()->data(index, Qt::UserRole+2).toInt();
if (nDataType == DelegateModel::EditAndCombox)
{
QString str = index.model()->data(index, Qt::UserRole+3).toString(); //获取关联的数据
QModelIndex no_const_index = const_cast<QModelIndex&>(index);
QString value = index.model()->data(index, Qt::DisplayRole).toString();
ComboboxLineedit* editor = new ComboboxLineedit(no_const_index, value, parent);
emit beginEdit();
return editor;
}
else
{
QLineEdit *editor = new QLineEdit(parent);
emit beginEdit();
return editor;
}
return nullptr;
}
// 为编辑器设置数据
void TableDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
int nDataType = index.model()->data(index, Qt::UserRole+2).toInt();
if (nDataType == DelegateModel::EditAndCombox)
{
ComboboxLineedit *edit = qobject_cast<ComboboxLineedit*>(editor);
if (edit)
{
edit->setText(index.model()->data(index, Qt::EditRole).toString());
}
}
else
{
QLineEdit *edit = qobject_cast<QLineEdit*>(editor);
if (edit)
{
edit->setText(index.model()->data(index, Qt::EditRole).toString());
}
}
}
// 将数据写入到模型
void TableDelegate::setModelData(QWidget *editor,
QAbstractItemModel *model, const QModelIndex &index) const
{
int nDataType = index.model()->data(index, Qt::UserRole+2).toInt();
if (nDataType == DelegateModel::EditAndCombox)
{
ComboboxLineedit *edit = qobject_cast<ComboboxLineedit*>(editor);
if (edit)
{
model->setData(index, edit->text());
// model->setData(index, edit->text(), Qt::UserRole);
}
}
else
{
QLineEdit *edit = qobject_cast<QLineEdit *>(editor);
if (edit)
{
model->setData(index, edit->text());
}
}
}