Results 1 to 7 of 7

Thread: Localizing date times in item views

  1. #1
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Localizing date times in item views

    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..

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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Localizing date times in item views

    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().

  3. The following user says thank you to wysota for this useful post:

    jpn (5th February 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Localizing date times in item views


  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Localizing date times in item views

    Quote Originally Posted by wysota View Post
    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
    J-P Nurmi

  6. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Localizing date times in item views

    Quote Originally Posted by wysota View Post
    Meanwhile TT is working on it, I decided to add an example to our wiki..
    J-P Nurmi

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Localizing date times in item views

    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?

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Localizing date times in item views

    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..
    J-P Nurmi

Similar Threads

  1. drag and drop with item views
    By castorvert in forum Qt Programming
    Replies: 14
    Last Post: 27th March 2006, 10:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.