How to represent QDateTime data in QTableView in needed format?
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.
Code:
for (int i=0; i<modelTable->rowCount(); i++)
{
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!
Re: How to represent QDateTime data in QTableView in needed format?
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
Re: How to represent QDateTime data in QTableView in needed format?
Re: How to represent QDateTime data in QTableView in needed format?
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?
Re: How to represent QDateTime data in QTableView in needed format?
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
Re: How to represent QDateTime data in QTableView in needed format?
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!