自定义QItemDelegate实现带CheckBox复选框的自增ID表格列
来源:互联网 发布:淘宝翻新机法律法规 编辑:程序博客网 时间:2024/05/17 23:48
查找并参阅了多位前辈的文章,最后总结如下:
新建C++类继承QItemDelegate,并覆写如下三个函数:
//绘制背景及内容、样式 void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;//创建编辑控件 QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;//鼠标、键盘事件处理 bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index);
1、绘制自增的编号/行号及复选框
void MyAutoIndexColumnDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{ QStyleOptionViewItem myOption = option; //绘制背景(默认蓝色底),不写这句的话,复选框背景无法显示高亮状态 drawBackground(painter, myOption,index); //绘制复选框,并根据index的Qt::CheckStateRole值改变状态 QRect r(myOption.rect.left()+3,myOption.rect.top()+myOption.rect.height()/2-8,16,16); Qt::CheckState checkstate=static_cast<Qt::CheckState>(index.data(Qt::CheckStateRole).toInt()); drawCheck(painter, myOption,r,checkstate); //序号居中显示,绘制时向右偏移一个值来避开Checkbox QRect r1(r.right()+3,myOption.rect.top(),myOption.rect.width()-20,myOption.rect.height()); myOption.displayAlignment = Qt::AlignHCenter | Qt::AlignVCenter; //绘制文本 drawDisplay(painter, myOption, r1,QString(" ")+ QString::number(index.row()+1)); //如果当前有焦点,就绘制一个焦点矩形,否则什么都不做 drawFocus(painter, myOption, myOption.rect);}
2、设置动作
bool MyAutoIndexColumnDelegate::editorEvent(QEvent *event,QAbstractItemModel *model,const QStyleOptionViewItem &option,onst QModelIndex &index){ if(event->type() == QEvent::KeyPress) return false; if(event->type() == QEvent::MouseButtonRelease) { QMouseEvent *mouse_event = static_cast<QMouseEvent*>(event); //只有左键点击才有效 if(mouse_event->button() != Qt::LeftButton) { return false; }// QRect r(option.rect.left(),option.rect.top(),20,20);// //直接点击复选框时才更改状态// if(!r.contains(mouse_event->pos()));// {// return false;// } //根据当前单元格的选中状态来在 选中/未选中 之间切换 QVariant value = index.data(Qt::CheckStateRole); Qt::CheckState state = (static_cast<Qt::CheckState>(value.toInt()) == Qt::Checked ? Qt::Unchecked : Qt::Checked); //设置当前单元格的选中状态 bool ok=model->setData(index,state, Qt::CheckStateRole); return ok; }}
3、设置列内容不可编辑
QWidget *MyAutoIndexColumnDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const{ return NULL;}
0 0
- 自定义QItemDelegate实现带CheckBox复选框的自增ID表格列
- 自定义表格添加复选框列
- 带复选框(checkbox)的树
- Android 自定义CheckBoxPreference的CheckBox复选框
- Android 自定义CheckBoxPreference的CheckBox复选框
- Extjs5实现带复选框的多选下拉选,复选框用extjs提供的checkbox样式
- Extjs5实现带复选框的多选下拉选,复选框用html本身的checkbox控件
- 设置gridview的checkbox列的值(gridview自带的checkbox列)
- Java中带复选框的树(Java CheckBox Tree)的实现和应用
- Java中带复选框的树(Java CheckBox Tree)的实现和应用
- Java中带复选框的树(Java CheckBox Tree)的实现和应用
- Bootstrap中表格内的checkbox复选框全选
- 复选框的实现(CheckBox和MenuItemImage)
- Android---自定义带CheckBox的ListView实现
- Android---自定义带CheckBox的ExpandableListView实现
- 自定义复选框checkbox样式
- CSS3自定义Checkbox复选框
- Android 自定义CheckBox复选框
- 一个工程如何直接引用另一个工程? 如何打包静态库.a? 如何打包静态库.frameWork? .a和.framework的区别?
- Linux Shell sort排序常用命令
- 磁盘阵列恢复方法以及注意事项
- 关于CollectionView的九宫格排布,多选,全选实现及删除
- 隐藏Boss——ddmlib使用入门
- 自定义QItemDelegate实现带CheckBox复选框的自增ID表格列
- 机器学习中使用的神经网络第二讲笔记
- 内建模块-HTMLParser
- 字符串模式匹配中BF算法和KMP算法的java实现
- android 基础 @SuppressLint("NewApi")和@TargetApi()的作用与区别
- 线段树
- sql基础知识-----主外键、参照完整性
- 个人能力怎么看?四个方面帮你量化UE&UI的绩效标准
- 关于swift - 网络请求+MD5,混合编程