PDA

View Full Version : Localizing date times in item views



jpn
5th February 2007, 17:35
Hello,

How does one correctly localize the date time format used in item views?

All I can see is QItemDelegate converting the display role variant to a string, where the QVariant conversion ends up using a hard coded value, Qt::ISODate, for converting the QDateTime. This gives me "YYYY-MM-DDTHH:MM:SS" which is not what I want. Oh, and setting the date time as text is out of question.. :p

What do you think? Have I missed something or is this possibly an overlook by the Trolls?

wysota
5th February 2007, 18:20
It's not the only place where the design is... questionable. The thing is that everywhere non-localised data is used unless explicitely specified to be localised. This includes dates, times, integer and real values. There should at least be a switch somewhere in QCoreApplication (or QApplication) telling the framework to localise (or not) all strings that can be localised.

As a solution for you I can only suggest subclassing QItemDelegate or QAbstractItemDelegate and implementing the functionality yourself. In case of QItemDelegate it'd probably be drawDisplay(), in case of its base class - paint().

wysota
5th February 2007, 19:08
By the way:
http://www.trolltech.com/developer/task-tracker/index_html?method=entry&id=102356

jpn
5th February 2007, 20:00
As a solution for you I can only suggest subclassing QItemDelegate or QAbstractItemDelegate and implementing the functionality yourself. In case of QItemDelegate it'd probably be drawDisplay(), in case of its base class - paint().
Yeah, thanks. For some reason subclassing QAbstractItemDelegate and implementing it all from scratch didn't quite attract me.. ;) With QItemDelegate::drawDisplay() it's rather straightforward to implement, but as a drawback it ends up doing somewhat excessive QVariant->QString->QDateTime->QString conversions. In addition, I had to use an ugly const_cast to be able to pick the type of the data being rendered as a member variable in paint() so it can later be used in drawDisplay() where the data is unfortunately passed as text.

Anyhow, here are the steps in case anyone in the future is struggling with the same problem:
- subclass QItemDelegate
- reimplement paint(): pick index.data().type() as a member variable (a const_cast is required since paint() is a const method) and then call the base class implementation
- reimplement drawDisplay(): check the type stored in the previous step and draw the data as localized if needed, otherwise call the base class implementation
- reimplement sizeHint(): calculate the size hint by hand in case the passed index refers to something being localized, otherwise call the base class implementation

jpn
5th February 2007, 22:27
By the way:
http://www.trolltech.com/developer/task-tracker/index_html?method=entry&id=102356
Meanwhile TT is working on it, I decided to add an example (http://wiki.qtcentre.org/index.php?title=Localizing_date_times_in_item_view s) to our wiki..

wysota
5th February 2007, 23:45
Great. If the code is short, maybe you could embed it directly in the wiki so that people can view it without downloading and decompressing the archive?

jpn
6th February 2007, 15:47
Ok, done. I felt it's a bit too long to include directly on the same page so I did some little organizing so that previews of the files are available as separate pages. I hope it's ok.. ;)