Chinaunix首页 | 论坛 | 博客
  • 博客访问: 299964
  • 博文数量: 101
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 150
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-07 07:42
文章分类

全部博文(101)

文章存档

2016年(12)

2015年(48)

2014年(41)

我的朋友

分类: C/C++

2015-07-24 08:34:17

QTableView的单元格内容实现还是继承了TableViewModel类的data(const QModelIndex &index, int role) const函数,那个设置颜色的问题也就在这个里面实现了。

1、设置某个单元格颜色

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();
}
阅读(1993) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~