forked from Xiaoyuan-Liu/Student-Information-Manage-System
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatedelegate.cpp
More file actions
61 lines (33 loc) · 1.56 KB
/
datedelegate.cpp
File metadata and controls
61 lines (33 loc) · 1.56 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
#include"datedelegate.h"
DateDelegate::DateDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
DateDelegate::~DateDelegate(){
}
QWidget *DateDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QDateTimeEdit *editer = new QDateTimeEdit(parent);
editer->setDisplayFormat("yyyy-MM-dd");//设置显示格式
editer->setCalendarPopup(true);//设置日历控件为悬空显示
editer->installEventFilter(const_cast<DateDelegate*>(this));//安装时间过滤器,使得代理能够获取定制控件的值
return editer;
}
void DateDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString dateStr = index.model()->data(index).toString();//通过index获取model中相应项ITem的值
QDate date = QDate::fromString(dateStr,Qt::ISODate);
QDateTimeEdit *edit = static_cast<QDateTimeEdit *>(editor);//转换类型获取定制的控件
edit->setDate(date);//将Item项中的值展示到定制控件中
}
void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QDateTimeEdit *edit = static_cast<QDateTimeEdit *>(editor);
QDate date = edit->date();
model->setData(index,QVariant(date.toString(Qt::ISODate)));//将定制控件中的值展示到表格中
}
//调整定制控件的展示,确保可以展示到窗体View中
void DateDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
editor->setGeometry(option.rect);
}