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() < )
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(( == index.column())&(fEntries[index.row()].value("LandType") == QString::fromLocal8Bit("登陆失败")))
{
return QVariant(Qt::GlobalColor(Qt::red));
}
else if((( == 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() < )
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( == 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() < )
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( == index.column())
{
return QVariant(Qt::GlobalColor(Qt::red));
}
}
return QVariant();
}

QTableView中修改某个单元格或者行或者列内容颜色的更多相关文章

  1. WPF中修改DataGrid单元格值并保存

    编辑DataGrid中的单元格的内容然后保存是非常常用的功能.主要涉及到的方法就是DataGrid的CellEditEnding  和BeginningEdit .其中BeginningEdit 是当 ...

  2. Easyui的datagrid的行编辑器Editor中添加事件(修改某个单元格带出其他单元格的值)

    项目中有个datagrid需要编辑行时,用到Editor的属性,那么如何添加一个事件 问题:同一个编辑行中的某个单元格值改变时,修改其他单元格的值 页面用到的datagrid <table id ...

  3. 【VBA】点击工作簿中的每个单元格,整行整列变色

    需求描述 在点击单元格的时候,单元格所在行与列都变色显示 实现方案 打开Excle表格,按住ALT+F11 双击上图中的ThisWorkbook Private Sub Workbook_SheetS ...

  4. table中tr间距的设定table合并单元格 colspan(跨列)和rowspan(跨行)

    table中的tr的默认display:table-row,虽然可以修改为display:block但是就失去了tr特有的显示效果,如(td自动对齐): 并且在tr中对起设定padding是有用的,可 ...

  5. 无法读取Excel中的数据单元格。有数据,但是读出来全是空值

    C#读取Excel,取值为空的解决办法! C#读取Excel遇到无法读取的解决方法是什么呢?这样在C#读取Excel的过程中有很多问题,那么本文就向你介绍如何解决C#读取Excel遇到无法读取的解决方 ...

  6. C# 获取Excel中的合并单元格

    C# 获取Excel中的合并单元格 我们在制作表格时,有时经常需要合并及取消合并一些单元格.在取消合并单元格时需要逐个查找及取消,比较麻烦.这里分享一个简单的方法来识别Excel中的合并单元格,识别这 ...

  7. iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建

    iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建 一.实现效果 说明:该示例在storyboard中使用动态单元格来完成. 二.实现 1.项目文件结构 ...

  8. NPOI.dll 用法。单元格,样式,字体,颜色,行高,宽度。读写excel

    NPOI.dll 用法.单元格,样式,字体,颜色,行高,宽度.读写excel 转载:http://yuncode.net/code/c_531e679b3896495 view source prin ...

  9. Excel 2010 Alt+; (分号) --- “只选定当前选定区域中的可视单元格”

    excel怎样把筛选出来的加上颜色? 1.选中筛选结果数据区域: 2.同时按下Alt+; (分号)键,选中筛选出的数据: 3.鼠标右键,设置单元格格式: 4.在弹出的对话框中,设置字体.背景颜色,即可 ...

随机推荐

  1. 编译可在Android上运行的qemu user mode

    前言 本文在Ubuntu 64位系统上对qemu项目进行交叉编译,并且只编译与qemu user mode有关的代码. 下文中的”NDK”若无特殊说明均指”Android NDK”. 下文中”$NDK ...

  2. Async All the Way

    Asynchronous code reminds me of the story of a fellow who mentioned that the world was suspended in ...

  3. Silverlight及WPF中实现自定义BusyIndicator

    在开发Silverlight或者WPF项目时,当我们调用Web服务来加载一些数据时,由于数据量比较大需要较长的时间,需要用户等待,为了给用户友好的提示和避免用户在加载数据过程中进行重复操作,我们通常使 ...

  4. Mysql 主从延时监控

    200 ? "200px" : this.width)!important;} --> 介绍 主从延时在主从环境中是一个非常值得关注的问题,有时候我们可以通过show sla ...

  5. C#+OpenGL+FreeType显示3D文字(3) - 用PointSprite绘制文字

    C#+OpenGL+FreeType显示3D文字(3) - 用PointSprite绘制文字 上一篇实现了把文字绘制到OpenGL窗口,但实质上只是把含有文字的贴图贴到矩形模型上.本篇我们介绍用Poi ...

  6. WCF服务的异常消息

    原创地址:http://www.cnblogs.com/jfzhu/p/4055024.html 转载请注明出处 WCF Service发生异常的时候,客户端一般只能看见这样一个错误:“The ser ...

  7. php后台修改人员表信息

    显示info人员表里所有内容 1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...

  8. 让easyui datagrid支持bootstrap的tooltip

    让easyui datagrid支持bootstrap的tooltip 发表于 下午 1:53 by ylpro.net & 分类 Java. Easyui在1.3.3版本之前是不支持tool ...

  9. SSIS Design7:数据类型的选择

    对于SSIS的数据类型,容易遗漏精度的是DateTime,不正确的使用DateTime,time,decimal 等数据类型,可能导致溢出或数据丢失. 1,Variable 和 Parameter的D ...

  10. 在Package中处理 bit column

    SQL Server没有boolean类型,使用bit 类型来代替,bit类型有两个值:0 和 1. SSIS package中有boolean类型,SSIS自动将bit 类型转换成boolean类型 ...