Answer the question
In order to leave comments, you need to log in
Why does it display the date incorrectly through QSqlTableModel?
I can not understand why it incorrectly gives the date stored in the SqlLite database through QSqlTableModel. It is necessary that the date be in the format dd.mm.yyyy, but it shows yyyy.mm.dd.
I created my own delegate that overrides editing, displaying, and drawing the date in the cell. In the table, the date is displayed as it should and edited as it should. But when trying to filter table records through QSortFilterProxyModel, for some reason it displays the date not as redefined in the delegate.
Overriding the DateEdit delegate:
DateEditDelegate::DateEditDelegate(QObject *parent)
: QItemDelegate(parent){}
QWidget *DateEditDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/*option*/,
const QModelIndex &/*index*/) const
{
QDateEdit *date = new QDateEdit(parent);
date->setFrame(false);
date->setMinimumDate(QDate(2015, 12, 01));
date->setMaximumDate(QDate(2099, 12, 01));
date->setDate(QDate::currentDate());
date->setCalendarPopup(true);
date->setDisplayFormat(dateFormat);
return date;
}
void DateEditDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QDate value = index.model()->data(index, Qt::EditRole).toDate();
QDateEdit *date = static_cast<QDateEdit*>(editor);
date->setDate(value);
}
void DateEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QDateEdit *date = static_cast<QDateEdit*>(editor);
date->interpretText();
model->setData(index, date->date(), Qt::EditRole);
}
void DateEditDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
{
editor->setGeometry(option.rect);
}
void DateEditDelegate::paint(QPainter *painter,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QStyleOptionViewItemV2 opt = setOptions(index, option);
opt.displayAlignment = Qt::AlignLeft | Qt::AlignVCenter;
drawDisplay(painter, opt, opt.rect, index.data(Qt::DisplayRole).toDate().toString(dateFormat));
pModelProxyFilter = new QSortFilterProxyModel;
pTableView->setModel(pModelProxyFilter);
pTableView->setItemDelegateForColumn(1, pDateDelegate);
qDebug() << pModelProxyFilter->data(pModelProxyFilter->index(2,1), Qt::DisplayRole).toString();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question