从QSqlQueryModel派生一个类,然后重新实现data()方法,将数值类型的列自定义重新格式化返回。下面是一个示例:
QVariant ChaoZhuan_QueryForm_SqlQueryModel::data(const QModelIndex & index, int role) const
{
QVariant value = QSqlQueryModel::data(index, role);
if (value.isValid() && role == Qt::DisplayRole)
{
switch (index.column())
{
case 3:
value = (value.toInt() == 1 ? "男" : "女");
break;
case 7:
value = (value.toInt() == 1 ? "市接收部门" : "区接收部门");
break;
case 4:
case 5:
case 18:
value = value.toDate().toString("yyyy-MM-dd");
break;
default:;
}
return value;
}
if (role == Qt::TextAlignmentRole && index.column() == 8)
{
value = (Qt::AlignVCenter + Qt::AlignRight);
return value;
}
return value;
}