-
Notifications
You must be signed in to change notification settings - Fork 0
/
dateDelegate.cpp
41 lines (39 loc) · 1.31 KB
/
dateDelegate.cpp
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 <QtGui>
#include <QDateEdit>
#include "dateDelegate.h"
DateDelegate::DateDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
QWidget *DateDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QDateEdit *editor = new QDateEdit(parent);
editor->setDisplayFormat("dd.MM.yy");
editor->setDate(QDate::currentDate());
return editor;
}
void DateDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QDate value = index.model()->data(index, Qt::EditRole).toDate();
QDateEdit *dateEdit = static_cast<QDateEdit*>(editor);
dateEdit->setDate(value);
}
void DateDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QDateEdit *dateEdit = static_cast<QDateEdit*>(editor);
QDate value = dateEdit->date();
model->setData(index, value, Qt::EditRole);
}
void DateDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}
QString DateDelegate::displayText(const QVariant &value, const QLocale &locale) const
{
return locale.toString(value.toDate(), "dd:MM:yy");
}