forked from Xiaoyuan-Liu/Student-Information-Manage-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelegate.cpp
More file actions
85 lines (77 loc) · 2.58 KB
/
delegate.cpp
File metadata and controls
85 lines (77 loc) · 2.58 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
84
85
#include "Delegate.h"
#include <QComboBox>
Delegate::Delegate(QObject *parent)
: QItemDelegate(parent)
{
}
Delegate::~Delegate()
{
}
void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QItemDelegate::paint(painter, option, index);
}
QSize Delegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
return QItemDelegate::sizeHint(option, index);
}
QWidget *Delegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.isValid() && (index.column() == SEXCOMBOXCOL||index.column()==ORIENTITIONCOMBOXCOL))
{
QComboBox *editor = new QComboBox(parent);
editor->setEditable(true);
editor->installEventFilter(const_cast<Delegate *>(this));
return editor;
}
else
{
return QItemDelegate::createEditor(parent, option, index);
}
}
void Delegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
if (index.isValid()){
if(index.column()==SEXCOMBOXCOL)
{
QString value = index.model()->data(index, Qt::DisplayRole).toString();
QComboBox *combox = static_cast<QComboBox *>(editor);
combox->addItem("(请选择)");
combox->addItem("男");
combox->addItem("女");
//combox->addItem("+3");
//combox->addItem("+4");
combox->setCurrentText(value);
}
else if(index.column()==ORIENTITIONCOMBOXCOL){
QString value = index.model()->data(index, Qt::DisplayRole).toString();
QComboBox *combox = static_cast<QComboBox *>(editor);
combox->addItem("(请选择)");
combox->addItem("计算机应用");
combox->addItem("软件工程");
combox->addItem("计算机科学");
combox->addItem("信息安全");
combox->addItem("计算机系统");
combox->setCurrentText(value);
}
}
else
{
QItemDelegate::setEditorData(editor, index);
}
}
void Delegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
if (index.isValid() && (index.column() == SEXCOMBOXCOL || index.column()==ORIENTITIONCOMBOXCOL))
{
QComboBox *combox = static_cast<QComboBox *>(editor);
model->setData(index, combox->currentText());
}
else
{
QItemDelegate::setModelData(editor, model, index);
}
}