PDA

View Full Version : How to represent QDateTime data in QTableView in needed format?



falconium
11th April 2011, 21:09
I have a source QStandardItemModel in which I have stored data for an object. Its first column has QDateTime values (modelTable->setData(modelTable->index(i, 2, QModelIndex()), timedate);).
I want to create a new model for a QTableView by copying modelTable data into modelChart model, but I also want to have the QDateTime variable displayed in different format, e.g. ISO or localized.

How can I do this? I haven't found the method for this. :(
I played with DisplayRole, but didn't help.


for (int i=0; i<modelTable->rowCount(); i++)
{
modelChart->setItem(i, 0, new QStandardItem());
modelChart->setData(modelChart->index(i, 0, QModelIndex()), modelTable->item(i, 2)->data(Qt::EditRole));
modelChart->setData(modelChart->index(i, 0, QModelIndex()), modelTable->item(i, 2)->data(Qt::EditRole), Qt::DisplayRole);
modelChart->setItem(i, 1, new QStandardItem(modelTable->item(i, 3)->text()));
modelChart->setData(modelChart->index(i, 1, QModelIndex()),QColor(r, g, b),Qt::DecorationRole);
}

Thanks in advance!

TomJoad
11th April 2011, 21:39
This may, or may not, be the answer you are looking for, but you can set a QDate/Time to a string: http://doc.qt.nokia.com/4.7/qdatetime.html#toString

If you need to manipulate it, you can set it back to a QDate: http://doc.qt.nokia.com/4.7/qdate.html#fromString

viulskiez
11th April 2011, 21:47
Use QLocale::toString.

falconium
11th April 2011, 22:50
Thank you, but it looks like these doesn't keep QDateTime type, but changes to QString. In this case, user cannot edit the cells as QDateTime (arrows to inc/dec values) and the Chart module (ChartXY) I'm using won't accept other format than QDateTime. :(
Any idea how to keep QDateTime with proper representation?

viulskiez
12th April 2011, 13:35
Try to keep QDateTime data type while setting the data for editing purpose (Qt::EditRole), and set the data for displaying (Qt::DisplayRole) with converted type (QString).

Edit: disable smilies

falconium
10th May 2011, 20:38
QVariant QStandardItem::data ( int role = Qt::UserRole + 1 ) const [virtual]
Returns the item's data for the given role, or an invalid QVariant if there is no data for the role.
Note: The default implementation treats Qt::EditRole and Qt::DisplayRole as referring to the same data.

Does it mean QStandardItem must be inherited to a new class that separates edit and display roles?
How can I do that if it is the case?

Thank you!