0
0
0142016-04-23 15:54:59
Qt
014, 2016-04-23 15:54:59

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));

We assign the model for filtering to QTableView:
pModelProxyFilter = new QSortFilterProxyModel;
pTableView->setModel(pModelProxyFilter);

Assigning a delegate to a date column:
pTableView->setItemDelegateForColumn(1, pDateDelegate);

Then I do this:
qDebug() << pModelProxyFilter->data(pModelProxyFilter->index(2,1), Qt::DisplayRole).toString();

And I get the date in the format yyyy.mm.dd. Why is this happening? After all, the delegate is overridden.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Demonist, 2016-04-26
@014

qDebug() << pModelProxyFilter->data(pModelProxyFilter->index(2,1), Qt::DisplayRole).toDate().toString("dd.MM.yyyy");

Method data method returns a QVariant and the delegate does not affect its content in any way. returns a value from your model.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question