分类: C/C++
2015-07-24 08:34:17
QVariant TableViewModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() >= fEntries.size() || index.row() < 0)
return QVariant();
if(role == Qt::DisplayRole) {
const Entry& entry = fEntries.at(index.row());
const QString& key = getColumnId(index.column());
return entry.value(key);
}
if(role == Qt::BackgroundRole)
{
if((1 == index.column())&(fEntries[index.row()].value("LandType") == QString::fromLocal8Bit("登陆失败")))
{
return QVariant(Qt::GlobalColor(Qt::red));
}
else if(((1 == index.column())&(fEntries[index.row()].value("LandType") == QString::fromLocal8Bit("登陆成功"))))
{
return QVariant(Qt::GlobalColor(Qt::green));
}
}
return QVariant();
}
我这个上面其实是有两种状态,根据里面的内容来显示颜色的变化,单元格的锁定时(index.column()和index.row()).
既然能锁定某个单个元格,那个锁定某一行或者一列也很简单。
2、设置某行颜色QVariant TableViewModel::data(const QModelIndex &index, int role) const{if (!index.isValid())return QVariant();if (index.row() >= fEntries.size() || index.row() < 0)return QVariant();if(role == Qt::DisplayRole) {const Entry& entry = fEntries.at(index.row());const QString& key = getColumnId(index.column());return entry.value(key);}
if(role == Qt::BackgroundRole){
if(1 == index.row())
{
return QVariant(Qt::GlobalColor(Qt::red));
}
}
return QVariant();}
3、设置某列颜色QVariant TableViewModel::data(const QModelIndex &index, int role) const{if (!index.isValid())return QVariant();if (index.row() >= fEntries.size() || index.row() < 0)return QVariant();if(role == Qt::DisplayRole) {const Entry& entry = fEntries.at(index.row());const QString& key = getColumnId(index.column());return entry.value(key);}
if(role == Qt::BackgroundRole){
if(1 == index.column()){
return QVariant(Qt::GlobalColor(Qt::red));
}
}
return QVariant();}