[Qt-interest] Modifying data representation in model/view

yoann buch yoann.buch at gmail.com
Sat Sep 5 11:48:46 CEST 2009


Hi,

I might have an answer but it implies to modify the model (though I'd be
very interested in knowing a solution
directly with the view).

I'd subclass QSqlTableModel and override the following virtual function
(defined in QAbstractItemModel):

virtual QVariant *data <http://qabstractitemmodel.html/#data>* ( const
QModelIndex & *index*, int *role* = Qt::DisplayRole ) const = 0

Inside you could write:

if (role == Qt::DisplayRole && index.column() == index_of_your_column_date)
{

QDate date = QSqlTableModel::data(item, role).toDate();

// return whatever you want according to the localtime

return date.toString(/*format*/);
} else {
    // any other role or column
    return QSqlTableModel::data(item, role);
}

Hope this will help.

Yoann Buch.

On Fri, Sep 4, 2009 at 12:49 PM, <jarkko.marjeta at tietosaab.com> wrote:

> Hi,
>
> I am starting to study the model/view concepts of Qt.
>
> I am using Qt 4.5.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 (UTC/Localtime), but I don't
> know where/how to implement such logic. I dont want the model to be affected
> in any way, only the data shown in the view.
>
> Could you please give me some pointers or point out an example about this.
> I looked at some the examples provided with Qt, but I did not notice this
> kind of change-of-representation-format.
>
> Thank you for your advice.
>
> BR,
>  Jarkko
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090905/1bb05f2b/attachment.html 


More information about the Qt-interest-old mailing list