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

jarkko.marjeta at tietosaab.com jarkko.marjeta at tietosaab.com
Mon Sep 7 15:00:15 CEST 2009


Hi,
Tried it out and this seems to work like a charm for my needs.
Many thanks for your help.
  Jarkko



________________________________
From: yoann buch [mailto:yoann.buch at gmail.com]
Sent: 5. syyskuuta 2009 12:49
To: Marjeta Jarkko
Cc: qt-interest at trolltech.com
Subject: Re: [Qt-interest] Modifying data representation in model/view

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<mailto: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<mailto: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/20090907/dd37445d/attachment.html 


More information about the Qt-interest-old mailing list