第四种方法是:实现QAbstractItemDelegate的paint()函数。

这种方法和《C++_GUI_Qt4_编程(第二版)》中第十章的自定义委托例子,画星星的作法是一样的,都是通过 QApplication::style()->drawControl(QStyle::CE_CheckBox,&check_box_style_option,painter);这个函数,只不过选择的元素是QCheckBox的。

使用Delegate Paint()函数 具体如下:

void QLineDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,const QModelIndex& index)const
{  bool checked = index.model()->data(index, Qt::DisplayRole).toBool();  if(index.column() == 0)  {  QStyleOptionButton check_box_style_option;  check_box_style_option.state |= QStyle::State_Enabled;  if(checked)  {  check_box_style_option.state |= QStyle::State_On;  }  else  {  check_box_style_option.state |= QStyle::State_Off;  }  check_box_style_option.rect = CheckBoxRect(option);  QApplication::style()->drawControl(QStyle::CE_CheckBox,&check_box_style_option,painter);  }  QStyledItemDelegate::paint(painter,option,index);  QStyleOptionViewItem itemOption(option);  if(itemOption.state & QStyle::State_HasFocus)  itemOption.state = itemOption.state ^ QStyle::State_HasFocus;  QStyledItemDelegate::paint(painter,itemOption,index);  QPen oldPen = painter->pen();  painter->setPen(pen);  painter->drawLine(option.rect.topRight(),option.rect.bottomRight());  painter->drawLine(itemOption.rect.bottomLeft(),itemOption.rect.bottomRight());  painter->setPen(oldPen);
}  

完整的源代码如下:

class QLineDelegate : public QStyledItemDelegate
{Q_OBJECT
public:QLineDelegate(QTableView* tableView);
protected:void paint(QPainter* painter,const QStyleOptionViewItem& option,const QModelIndex& index) const;bool editorEvent(QEvent *event,QAbstractItemModel *model,const QStyleOptionViewItem &option,const QModelIndex &index) ;
private:QPen         pen;QTableView*  view;
};static QRect CheckBoxRect(const QStyleOptionViewItem &view_item_style_options)
{QStyleOptionButton check_box_style_option;QRect check_box_rect = QApplication::style()->subElementRect(QStyle::SE_CheckBoxIndicator,&check_box_style_option);QPoint check_box_point(view_item_style_options.rect.x() +view_item_style_options.rect.width() / 2 -check_box_rect.width() / 2,view_item_style_options.rect.y() +view_item_style_options.rect.height() / 2 -check_box_rect.height() / 2);return QRect(check_box_point, check_box_rect.size());
}QLineDelegate::QLineDelegate(QTableView* tableView)
{int gridHint = tableView->style()->styleHint(QStyle::SH_Table_GridLineColor, new QStyleOptionViewItemV4());QColor gridColor = static_cast<QRgb>(gridHint);pen = QPen(gridColor,0,tableView->gridStyle());view = tableView;
}void QLineDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,const QModelIndex& index)const
{bool checked = index.model()->data(index, Qt::DisplayRole).toBool();if(index.column() == 0){QStyleOptionButton check_box_style_option;check_box_style_option.state |= QStyle::State_Enabled;if(checked){check_box_style_option.state |= QStyle::State_On;}else{check_box_style_option.state |= QStyle::State_Off;}check_box_style_option.rect = CheckBoxRect(option);QApplication::style()->drawControl(QStyle::CE_CheckBox,&check_box_style_option,painter);}QStyledItemDelegate::paint(painter,option,index);QStyleOptionViewItem itemOption(option);if(itemOption.state & QStyle::State_HasFocus)itemOption.state = itemOption.state ^ QStyle::State_HasFocus;QStyledItemDelegate::paint(painter,itemOption,index);QPen oldPen = painter->pen();painter->setPen(pen);painter->drawLine(option.rect.topRight(),option.rect.bottomRight());painter->drawLine(itemOption.rect.bottomLeft(),itemOption.rect.bottomRight());painter->setPen(oldPen);
}bool QLineDelegate::editorEvent(QEvent *event,QAbstractItemModel *model,const QStyleOptionViewItem &option,const QModelIndex &index) {if((event->type() == QEvent::MouseButtonRelease) ||(event->type() == QEvent::MouseButtonDblClick)){QMouseEvent *mouse_event = static_cast<QMouseEvent*>(event);if(mouse_event->button() != Qt::LeftButton ||!CheckBoxRect(option).contains(mouse_event->pos())){return false;}if(event->type() == QEvent::MouseButtonDblClick){return true;}}else if(event->type() == QEvent::KeyPress){if(static_cast<QKeyEvent*>(event)->key() != Qt::Key_Space &&static_cast<QKeyEvent*>(event)->key() != Qt::Key_Select){return false;}}else{return false;}bool checked = index.model()->data(index, Qt::DisplayRole).toBool();return model->setData(index, !checked, Qt::EditRole);
}

不过有一个小问题,CheckBox旁边有false/true或其它字符的原因是:你的Model中的data返回值,应该排除这列。

转:http://www.cppblog.com/gaimor/archive/2011/11/26/160993.html

【Qt】QTableView中嵌入复选框CheckBox 的四种方法总结

(四)Paint函数实现QTableView中增加QCheckBox的方法相关推荐

  1. php如何向数组增加数据,php向数组中增加数据的方法是什么

    php向数组中增加数据的方法是什么? 使用函数array_push array_push()函数向第一个参数的数组尾部添加一个或多个元素(入栈),然后返回新数组的长度. 该函数等于多次调用 $arra ...

  2. python如何增加字符串_在python中增加字符的方法

    在本教程中,我们将看到在Python中增加字符的不同方法. 类型转换 首先让我们看看如果在不进行类型转换的情况下向char添加int会发生什么. 示例## str initialization cha ...

  3. qt 子类调用父类的函数_子类中调用父类的方法

    父类名 . __init__(self, ...) 可以将父类中的init中的属性重复调用,减少代码的重复 class Vehicle: def __init__(self, name, speed, ...

  4. python中的strptime函数_python中strptime函数_Python语言中操作时间之strptime()方法的使用...

    Python中操作时间之strptime()方法的使用 strptime()方法分析表示根据格式的时间字符串.返回值是一个struct_time所返回gmtime()或localtime(). 格式参 ...

  5. allegro怎么设置孔的属性_两种在Allegro中增加过孔的方法

    Input Pad name: 在 pad 库里面选择希望用来做过孔的 pad. Select end layer: Bottom 层, Add prefix 则可以输入你希望命名的过孔的名字. la ...

  6. linux虚拟服务器新增磁盘怎么挂载,如何在vmware虚拟机Linux中增加硬盘的方法(教程)...

    前期准备:创建虚拟硬盘 1.关闭VM中正在运行的虚拟系统: 2.在虚拟系统名称上点右键->Virtual Machine Settings: 3.在Hardware页点"Add&quo ...

  7. java 方法 函数 区别_Java中的构造函数和方法之间的区别

    Java方法一种方法用于探索对象的行为. 我们可以在方法的前面加上访问修饰符. 方法必须具有返回类型,例如void,任何原始类型(int,char,float等),任何Object类型(Integer ...

  8. linq绑定下拉列表,combobox中增加listitem的方法,增加“请选择”

    第一步:自定义一个类ListItem public class ListItem     {         private string text = string.Empty;         p ...

  9. 转载:QTableView中嵌入可视化组件

    出处:http://qimo601.iteye.com/blog/1538364 QTableView中嵌入可视化组件方法有四种: 第一种不能之前显示,必须双击/选中后才能显示,不适用. 第二种比较简 ...

最新文章

  1. CEO亲自写代码登上热榜,从零开始打造“裸金属”树莓派
  2. 困扰爱因斯坦的「幽灵般的超距作用」,是如何被贝尔定理证明确实存在的?...
  3. scss安装_安装了这9个免费PS插件后,终于能正常时间下班了……
  4. 音视频技术开发周刊 | 220
  5. iframe悬浮在html上_HTML Iframe
  6. 传智播客 c#_播客#46:Alexander Kallaway
  7. 求圆周率π一万位程序分析
  8. goahead webserver源码分析
  9. 贪吃的九头龙-----树形dp
  10. 分布式环境时钟同步问题
  11. 使用Visual Studio编译Qt项目出现“E1696 无法打开Qt源文件”的解决方案,亲测可行
  12. 求集合中最大值、最小值、和
  13. 什么是memcache,常见的memcache方法
  14. redis原理快速入门知识点总结
  15. BZOJ 4668 冷战(按秩合并并查集+LCA)
  16. 电磁场与电磁波(10)——电容与部分电容
  17. 区块链游戏,只是一场游戏….
  18. android双清步骤,安卓刷机前双清如何操作?华为荣耀3C双清的过程
  19. 将Visio图片导入到Latex
  20. 文件IO编程之学习总结

热门文章

  1. Python入门-散点图绘制
  2. Yii2性能优化之:缓存依赖
  3. javascript获取Select下拉框的值
  4. 【python】循环结构if、for 、while
  5. 【Java】关于Java的一些基础知识点
  6. C#中的字符与字符串
  7. python基础之类的属性方法、魔术方法普通方法
  8. 04-12 常见接口安全问题及解决方案
  9. 前端遇到瓶颈了怎么办?
  10. 前端工程师成长之多读好书 1