I realise I'm a bit late into this thread but I found it looking for solutions to similar issues with Sqlite "dates". Maybe this post will be useful to others.
Sqlite will treat text in an ISO date format as a date for the purposes of its date functions. However, when it reports the data in the "date" column to Qt you end up with a QVariant::String. Some Qt components recognise the ISO date string and treat it as a date, some don't. I'd like everything to have the best chance of just doing the Right Thing. How I am tackling this is to create a model sub-class that overrides data() to convert the QVariant::String to QVariant::Date on relevant columns:
{
// Default return value
// Convert date column into real date type
if (role == Qt::EditRole || role == Qt::DisplayRole)
if (item.column() == dateIssued)
return ret;
}
QVariant MyTableModel::data ( const QModelIndex & item, int role ) const
{
// Default return value
QVariant ret = QSqlTableModel::data(item, role);
// Convert date column into real date type
if (role == Qt::EditRole || role == Qt::DisplayRole)
if (item.column() == dateIssued)
if (ret.canConvert(QVariant::Date))
ret.convert(QVariant::Date);
return ret;
}
To copy to clipboard, switch view to plain text mode
I'm still pondering whether I need to do something with setData() for these columns, but at the moment it doesn't seem to be needed. I end up with an ISO date in the column if I pass setData() a QVariant::Date.
Bookmarks