方法:
创造model类继承QSqlTableModel,然后重载data函数
头文件:
#ifndef INCOME_MODEL_H
#define INCOME_MODEL_H
#include <QSqlTableModel>
class Income_Model : public QSqlTableModel
{
public:
Income_Model(QObject * parent = 0, QSqlDatabase db = QSqlDatabase());
~Income_Model();
QVariant data(const QModelIndex &idx, int role = Qt::DisplayRole) const;
};
#endif // INCOME_MODEL_H
源文件:
#include "income_model.h"
#include <QColor>
#include <QFont>
Income_Model::Income_Model(QObject * parent, QSqlDatabase db) : QSqlTableModel(parent,db)
{
}
Income_Model::~Income_Model()
{
}
QVariant Income_Model::data(const QModelIndex &idx, int role) const
{
QVariant value = QSqlTableModel::data(idx,role);
if(Qt::TextAlignmentRole == role)
{
value = int(Qt::AlignCenter | Qt::AlignHCenter);
return value;
}
if(Qt::ForegroundRole == role)
{
if(3 == idx.column())
return QVariant(QColor(255,144,144));
}
if(Qt::FontRole == role)
{
if(3 == idx.column())
{
QFont font;
font.setBold(true);
return QVariant(font);
}
}
return value;
}
其中:
Qt::TextAlignmentRole控制对齐方式
Qt::ForegroundRole控制前景色
Qt::FontRole控制字体
....