-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdateeditdelegate.cpp
More file actions
41 lines (33 loc) · 1.33 KB
/
dateeditdelegate.cpp
File metadata and controls
41 lines (33 loc) · 1.33 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
#include "dateeditdelegate.h"
#include "qdatetimeedit.h"
DateEditDelegate::DateEditDelegate(QObject *parent)
: QItemDelegate{parent}
{}
QWidget *DateEditDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QDateEdit * editor= new QDateEdit(parent);
editor->setDisplayFormat("yyyy/MM/dd");
return editor;
}
void DateEditDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QString value = index.model()->data(index, Qt::DisplayRole).toString();
QDateEdit *dateEdit = static_cast<QDateEdit*>(editor);
dateEdit->setDate(QDate::fromString(value, "yyyy/MM/dd"));
}
void DateEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QDateEdit *dateEdit = static_cast<QDateEdit*>(editor);
// lineEdit->returnPressed();
QString value = dateEdit->text();
model->setData(index, value, Qt::EditRole);
}
void DateEditDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}