1

I am trying to display datetime values in a QTableView. I have found this working pyside2 example (scroll down) for string and float type data:

PySide + QTableView example

What would I need to change within the table model so that I could display datetime data. How could this data be formatted to be displayed for example like '01.05.2019'.

I do NOT want to convert the datetime data to string beforehand since then the data cannot be sorted in a meaningful way when clicking in the table header...

Thanks a lot!

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Fab
  • 213
  • 3
  • 15

1 Answers1

2

Just return the data as a QDateTime (or QDate or QTime). The QTableView should be fine with that.

If you want to format the date differently then it starts to get complicated: you'll need to convert it to a string using your own formatting. Then to get the sorting right, you'll need to return the original date data in some other role (Qt::UserRole) and set that to be the sort role, as explained in this answer (which also suggests an alternative approach using a delegate).

strubbly
  • 3,347
  • 3
  • 24
  • 36