0

I am using Qt 5.4.2 and I have trouble in trying to find out, how to change data "representation" when populating a view from model. I have a QSqlTableModel to hold the database data and a QTableView that is displaying the contents of the database.

Now some of the data in the database is in a different format than what I would like to show in the View. For example I would like to show date formats differently, but I don't know where/how to implement such logic. I don't want the model to be affected in any way, only the data shown in the view.

halfer
  • 19,824
  • 17
  • 99
  • 186
user55340
  • 87
  • 1
  • 6

1 Answers1

0

You should implement a custom delegate for the specific column and set it manually. Simply implement a class derived from QStyledItemDelegate like :

#include <QItemDelegate>
#include <QDateTimeEdit>

class DateTimeEditDelegate: public QStyledItemDelegate
{
 Q_OBJECT
public:
    DateTimeEditDelegate(QObject *parent = 0);

    void *paint( QPainter *painter,
                            const QStyleOptionViewItem &option,
                            const QModelIndex &index ) const;

    QWidget *createEditor( QWidget *parent,
                            const QStyleOptionViewItem &option,
                            const QModelIndex &index ) const;

    void setEditorData( QWidget *editor,
                            const QModelIndex &index ) const;

    void setModelData( QWidget *editor,
                            QAbstractItemModel *model,
                            const QModelIndex &index ) const;

    void updateEditorGeometry( QWidget *editor,
                            const QStyleOptionViewItem &option,
                            const QModelIndex &index ) const;

    mutable QDateTimeEdit *dataTimeEdit;

private slots:

    void setData(QDateTime val);

};



DateTimeEditDelegate::DateTimeEditDelegate(QObject *parent ):QStyledItemDelegate(parent)
{

}

void*DateTimeEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QDateTime dateTime = index.model()->data( index, Qt::DisplayRole ).toDateTime();
    painter->drawText(option.rect().topLeft(), dateTime.toString());
    return;
}

QWidget *DateTimeEditDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    dataTimeEdit = new QDateTimeEdit( parent );
    QObject::connect(dataTimeEdit,SIGNAL(dateTimeChanged(QDateTime)),this,SLOT(setData(QDateTime)));
    return dataTimeEdit;
}

void DateTimeEditDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QVariant dateTime = index.model()->data( index, Qt::DisplayRole );

    (static_cast<QDateTimeEdit*>( editor ))->setDateTime(dateTime.toDateTime());
}

void DateTimeEditDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    model->setData( index, static_cast<QDateTimeEdit*>( editor )->dateTime() );
}


void DateTimeEditDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry( option.rect );
}

void DateTimeEditDelegate::setData(QDateTime val)
{
    emit commitData(dataTimeEdit);
}

And finally set an instance of the delegate to a column:

ui->tableView->setItemDelegateForColumn(0, new DateTimeEditDelegate(ui->tableView));

It's just a sample for a delegate which implements custom data representation along custom data editing. You should implement you own based on your needs.

Nejat
  • 31,784
  • 12
  • 106
  • 138