Qt World Summit: Submit your Presentation

Time format in QTableView with QSqlTableModel

  • Hello all,

    I have QSqlTableModel connected to MySQL table where one column has type 'time'.
    This column is used to store amount of time and I need to show it in hh:mm:ss format.
    But in connected QTableView it is shown as 12:10 AM instead of 00:10:00.

    Is there any way how to change the time format for this?

    Thanks in advance,


  • I think changing your locale to a locale that uses that format by default (at least for that QTableView widget) would do it.

  • Thank You Andre for help. Locale change helped (slightly). Now it is displayed as 'h:mm'. But I need also seconds - format 'hh:mm:ss'.

    Do you know how to do it?

  • Other than that, I think your best bet is to subclass [[doc:QStyledItemDelegate]] and reimplement the displayText() method. You just return the time in the format you like from there.

  • @QString QTime::toString ( const QString & format ) const@

    Format Result
    hh:mm:ss.zzz 14:13:09.042
    h:m:s ap 2:13:9 pm
    H:m:s a 14:13:9 pm

Log in to reply