I want to sort a table by dates. The problem is that they are interpreted as strings, hence my local date format is sorted wrongly, like 26. September is bigger than 16. November, because 26 > 16.
Anyway, so I have my own model set up and tried it like this:
QVariant MyModel::data(const QModelIndex &index, int role) const
{
if(role == Qt::UserRole)
{
if(index.column() == 5) // Date
return QSqlTableModel::data(index, role).toDate();
}
if(role == Qt::DisplayRole)
{
if(index.column() == 5) // Date
return QSqlTableModel::data(index.role).toDate().toString("dd MMMM yyyy");
}
}
and I set the sortRole like this:
proxyModel->setSortRole(Qt::UserRole);
The corresponding lines actually get called, but now I cant sort the table at all. It's just not responding. The arrows (representing asc or desc ordering) at the corresponding columns are changing, but the data isn't. Of course I set the rest like:
proxyModel->setDynamicSortFilter(true);
proxyModel->setSourceModel(myDBModel);
proxyModel->setFilterKeyColumn(1);
proxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
If I do not set the sortRole at least I can sort by the other columns correctly.
What am I doing wrong here? Do I have to implement another function or anything? I looked around the internet, but all I found was problems with sorting integers from years back, never dates :(