算了,还是把代码贴出来吧:
//mytablemodel.h
#include <QSqlTableModel>
class MyTableModel: public QSqlTableModel
{
Q_OBJECT
public:
explicit MyTableModel(QObject *parent = 0);
QVariant data(const QModelIndex & index, int role = Qt::DisplayRole) const;
};
//mytablemodel.cpp
#include "mytablemodel.h"
MyTableModel::MyTableModel(QObject *parent) :
QSqlTableModel(parent)
{
}
QVariant MyTableModel::data(const QModelIndex & index,
int role = Qt::DisplayRole) const
{
if (!index.isValid())
return QVariant();
QVariant vt = QSqlTableModel::data(index, role);
if (QVariant::DateTime == vt.type())
return vt.toDate().toString("yyyy-MM-dd");
return vt;
}
然后你使用MyTableModel就可以了,没有编译过,可能会有小错误。