Localizing dates and times in item views
From QtCentreWiki
| This article is obsolete since version 4.3. QItemDelegate is fully locale-aware since Qt 4.3. |
Problem
In Qt versions 4.2 and older, how to localize dates and times in item views while proper sorting and editing is retained?
Solution
- Rendering
- To make dates and times rendered as localized, make your model return them as localized strings for Qt::DisplayRole. This will make views calculate sufficient sizes for localized dates and times out of the box.
- Editing
- To make suitable editors appear for dates and times, make your model return actual dates and times for Qt::EditRole:
| Data type | Editor |
|---|---|
| QDate | QDateEdit |
| QTime | QTimeEdit |
| QDateTime | QDateTimeEdit |
- Sorting
- For proper sorting, set Qt::EditRole as QSortFilterProxyModel's sortRole.
- Example
QVariant Model::data(const QModelIndex& index, int role) const
{
...
switch (role)
{
case Qt::DisplayRole:
return dateTime.toString(...);
case Qt::EditRole:
return dateTime;
...
}
}
jpn 15:29, 10 July 2007 (CEST)

