Qt自定义委托在QTableView中绘制控件、图片、文字
自定义委托,继承于,QStyledItemDelegate类,重载Paint()函数,
1、实现在QTableView中绘制 格式字符串
2、实现在QTableView中绘制进度条
3、实现在QTableView中绘制QCheckBox
4、实现在QTableView中绘制星星
5、实现在QTableView中绘制Pixmap图片


1、实现在QTableView中绘制 格式字符串
- //重载绘制函数
- void DelReconQueue::paint(QPainter *painter, const QStyleOptionViewItem &option,
- const QModelIndex &index) const
- {
- //如果是第2列'病人Id'
- if (index.column() == 2)
- {
- //获得当前项值
- int patientId = index.model()->data(index, Qt::DisplayRole).toInt();
- //设置'病人Id'格式字符串: P:00000x;6位10进制数,不足补0;
- QString text = QString("P:%1").arg(patientId, 6, 10, QChar('0'));
- //获取项风格设置
- QStyleOptionViewItem myOption = option;
- myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
- //绘制文本
- QApplication::style()->drawItemText ( painter, myOption.rect , myOption.displayAlignment, QApplication::palette(), true,text );
- }
- else
- {
- //否则调用默认委托
- QStyledItemDelegate::paint(painter, option, index);
- }
- }
如果自定义委托继承于QItemDelegate类,绘制字符串,可以直接用QItemDelegate中的drawDisplay();
- void TrackDelegate::paint(QPainter *painter,
- const QStyleOptionViewItem &option,
- const QModelIndex &index) const
- {
- //保存音轨的列
- if (index.column() == durationColumn) {
- //获得索引对应Model中的数据
- int secs = index.model()->data(index, Qt::DisplayRole).toInt();
- //设置时间格式字符串 分:秒
- QString text = QString("%1:%2")
- .arg(secs / 60, 2, 10, QChar('0'))
- .arg(secs % 60, 2, 10, QChar('0'));
- //获取项风格设置
- QStyleOptionViewItem myOption = option;
- myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
- //绘制文本
- drawDisplay(painter, myOption, myOption.rect, text);
- //如果当前有焦点,就绘制一个焦点矩形,否则什么都不做
- drawFocus(painter, myOption, myOption.rect);
- } else{
- //否则默认
- QItemDelegate::paint(painter, option, index);
- }
- }
2、实现在QTableView中绘制进度条
- //重载绘制函数
- void DelReconQueue::paint(QPainter *painter, const QStyleOptionViewItem &option,
- const QModelIndex &index) const
- {
- //如果是'已经完成子任务数'
- if (index.column() == 9)
- {
- const QAbstractItemModel *itemModel = index.model();
- //获得索引对应Model中的数据
- int finishedSubTaskNum = itemModel->data(index, Qt::DisplayRole).toInt();
- int subTaskNum = itemModel->data(itemModel->index(index.row(),8), Qt::DisplayRole).toInt();
- //进度条的风格选项
- QStyleOptionProgressBarV2 *progressBarOption = new QStyleOptionProgressBarV2();
- progressBarOption->rect = option.rect;
- progressBarOption->minimum = 0;
- progressBarOption->maximum = subTaskNum;
- progressBarOption->progress = finishedSubTaskNum;
- int t = finishedSubTaskNum/subTaskNum;
- progressBarOption->text = QString::number(t) + "%";
- progressBarOption->textVisible = true;
- //绘制进度条
- QApplication::style()->drawControl(QStyle::CE_ProgressBar, progressBarOption, painter);
- }
- else
- {
- //否则调用默认委托
- QStyledItemDelegate::paint(painter, option, index);
- }
- }
3、实现在QTableView中绘制QCheckBox
- #include <QtGui>
- #include <QItemDelegate>
- #include <QStyleOptionProgressBarV2>
- #include "DelReconQueue.h"
- //重载绘制函数
- void DelReconQueue::paint(QPainter *painter, const QStyleOptionViewItem &option,
- const QModelIndex &index) const
- {
- if (index.column() == 11)
- {
- //获取值
- bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
- //按钮的风格选项
- QStyleOptionButton *checkBoxOption = new QStyleOptionButton();
- checkBoxOption->state |= QStyle::State_Enabled;
- //根据值判断是否选中
- if(checked)
- {
- checkBoxOption->state |= QStyle::State_On;
- }
- else
- {
- checkBoxOption->state |= QStyle::State_Off;
- }
- //返回QCheckBox几何形状
- checkBoxOption->rect = CheckBoxRect(option);
- //绘制QCheckBox
- QApplication::style()->drawControl(QStyle::CE_CheckBox,checkBoxOption,painter);
- }
- else
- {
- //否则调用默认委托
- QStyledItemDelegate::paint(painter, option, index);
- }
- }
//生成QCheckBox
- QRect DgSystemLog::CheckBoxRect(const QStyleOptionViewItem &viewItemStyleOptions)const
- {
- //绘制按钮所需要的参数
- QStyleOptionButton checkBoxStyleOption;
- //按照给定的风格参数 返回元素子区域
- QRect checkBoxRect = QApplication::style()->subElementRect( QStyle::SE_CheckBoxIndicator, &checkBoxStyleOption);
- //返回QCheckBox坐标
- QPoint checkBoxPoint(viewItemStyleOptions.rect.x() + viewItemStyleOptions.rect.width() / 2 - checkBoxRect.width() / 2,
- viewItemStyleOptions.rect.y() + viewItemStyleOptions.rect.height() / 2 - checkBoxRect.height() / 2);
- //返回QCheckBox几何形状
- return QRect(checkBoxPoint, checkBoxRect.size());
- }
4、实现在QTableView中绘制自定义类 星星
- //重载绘制函数
- void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
- const QModelIndex &index) const
- {
- //如果某项数据是星星类型
- if (qVariantCanConvert<StarRating>(index.data())) {
- //获取该项数据,并转换成StarRating类型
- StarRating starRating = qVariantValue<StarRating>(index.data());
- //如果有控件被选中,我们就让选中的控件变亮
- if (option.state & QStyle::State_Selected)
- painter->fillRect(option.rect, option.palette.highlight());
- starRating.paint(painter, option.rect, option.palette,
- StarRating::ReadOnly);
- }
- //如果没有控件选中,调用默认委托
- else {
- QStyledItemDelegate::paint(painter, option, index);
- }
- }
星星自定义源文件
- #ifndef STARRATING_H
- #define STARRATING_H
- #include <QMetaType>
- #include <QPointF>
- #include <QVector>
- class StarRating
- {
- public:
- enum EditMode { Editable, ReadOnly };
- StarRating(int starCount = 1, int maxStarCount = 5);
- void paint(QPainter *painter, const QRect &rect,
- const QPalette &palette, EditMode mode) const;
- QSize sizeHint() const;
- int starCount() const { return myStarCount; }
- int maxStarCount() const { return myMaxStarCount; }
- void setStarCount(int starCount) { myStarCount = starCount; }
- void setMaxStarCount(int maxStarCount) { myMaxStarCount = maxStarCount; }
- private:
- QPolygonF starPolygon;
- QPolygonF diamondPolygon;
- int myStarCount;
- int myMaxStarCount;
- };
- //让所有模板类型都知道该类,包括QVariant
- Q_DECLARE_METATYPE(StarRating)
- #endif
- #include <QtGui>
- #include <math.h>
- #include "starrating.h"
- const int PaintingScaleFactor = 20;
- StarRating::StarRating(int starCount, int maxStarCount)
- {
- myStarCount = starCount;
- myMaxStarCount = maxStarCount;
- starPolygon << QPointF(1.0, 0.5);
- for (int i = 1; i < 5; ++i)
- starPolygon << QPointF(0.5 + 0.5 * cos(0.8 * i * 3.14),
- 0.5 + 0.5 * sin(0.8 * i * 3.14));
- diamondPolygon << QPointF(0.4, 0.5) << QPointF(0.5, 0.4)
- << QPointF(0.6, 0.5) << QPointF(0.5, 0.6)
- << QPointF(0.4, 0.5);
- }
- QSize StarRating::sizeHint() const
- {
- return PaintingScaleFactor * QSize(myMaxStarCount, 1);
- }
- void StarRating::paint(QPainter *painter, const QRect &rect,
- const QPalette &palette, EditMode mode) const
- {
- painter->save();
- painter->setRenderHint(QPainter::Antialiasing, true);
- painter->setPen(Qt::NoPen);
- if (mode == Editable) {
- painter->setBrush(palette.highlight());
- } else {
- painter->setBrush(palette.foreground());
- }
- int yOffset = (rect.height() - PaintingScaleFactor) / 2;
- painter->translate(rect.x(), rect.y() + yOffset);
- //画笔坐标
- painter->scale(PaintingScaleFactor, PaintingScaleFactor);
- for (int i = 0; i < myMaxStarCount; ++i) {
- if (i < myStarCount) {
- painter->drawPolygon(starPolygon, Qt::WindingFill);
- } else if (mode == Editable) {
- painter->drawPolygon(diamondPolygon, Qt::WindingFill);
- }
- painter->translate(1.0, 0.0);
- }
- painter->restore();
- }
5、实现在QTableView中绘制Pixmap图片 ,
详细请看具体例子 Qt-在表格(QTableView)中插入图片
- void MyItemDelegate::paint(QPainter * painter,
- const QStyleOptionViewItem & option,
- const QModelIndex & index) const
- {
- if(index.column()!=0){
- QItemDelegate::paint(painter,option,index);
- return;
- }
- const QAbstractItemModel * model=index.model();
- QVariant var=model->data(index,Qt::CheckStateRole);
- if(var.isNull()) var=false;
- const QPixmap & star=var.toBool()?
- favouritePixmap:notFavouritePixmap;
- int width=star.width();
- int height=star.height();
- QRect rect=option.rect;
- int x=rect.x()+rect.width()/2-width/2;
- int y=rect.y()+rect.height()/2-height/2;
- painter->drawPixmap(x,y,star);
- }
Qt自定义委托在QTableView中绘制控件、图片、文字的更多相关文章
- Qt自定义委托在QTableView中绘制控件、图片、文字(内容比较全)
自定义委托,继承于,QStyledItemDelegate类,重载Paint()函数, 1.实现在QTableView中绘制 格式字符串 2.实现在QTableView中绘制进度条 3.实现在QTab ...
- Qt自定义阴影效果和QOpenGLWidget冲突导致控件不刷新
Qt5.6.2版本存在这样一个问题(其它版本未测试),当main函数中设置了application.setAttribute(Qt::AA_NativeWindows)(用于使得每个子界面都可以获取w ...
- Winform中Picture控件图片的拖拽显示
注解:最近做了一个小工具,在Winform中对Picture控件有一个需求,可以通过鼠标从外部拖拽图片到控件的上,释放鼠标,显示图片! 首先你需要对你的整个Fom窗口的AllowDrop设置Ture ...
- 在Paint事件中绘制控件(边框)
单纯的自己记录,将来会继续添加,侥幸被大家发现了的话请不要太鄙视... private void panel4_Paint(object sender, PaintEventArgs e) { Con ...
- paip.提升用户体验---c++ qt自定义窗体(1)---标题栏的绘制
源地址:http://blog.csdn.net/attilax/article/details/12343625 paip.提升用户体验---c++ qt自定义窗体(1)---标题栏的绘制 效果图: ...
- 在WinForm中使用委托来在其他线程中改变控件的显示
假设winform中有两个控件: 1.ListView用来显示进度的文本提示,ID:listView_progressInfo 2.ProgressBar用来显示进度,ID:progressBar1 ...
- Qt在表格中加入控件
任务:使用QTableWidget动态生成表格,在每行的某两列中加入QComboBox下拉框控件和QPushButton按钮控件 有添加,删除,编辑功能,每行的按钮可以浏览文件夹并选择文件 1.新建一 ...
- WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探
原文:WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探 最近因为项目需要,开始学习如何使用WPF开发桌面程序.使用WPF一段时间之后,感 ...
- Kotlin 第一弹:自定义 ViewGroup 实现流式标签控件
古人学问无遗力, 少壮工夫老始成.纸上得来终觉浅, 绝知此事要躬行. – 陆游 <冬夜读书示子聿> 上周 Google I/O 大会的召开,宣布了 Kotlin 语言正式成为了官方开发语言 ...
随机推荐
- 让一个父级div根据子级div高度而自适应高度
需求是点击上传的时候进行子级div高度不定,相对来说父级div高度也不能固定,把元素都设置成普通标准流,然后样式可以使用margin内边距或者padding外边距来进行调节 放上代码供参考: .opu ...
- Computer Vision_33_SIFT:Object recognition from local scale-invariant features——1999
此部分是计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面.对于自己不太熟悉的领域比如摄像机标定和立体视觉,仅仅列出上google上引用次数比较多的文献.有一些刚刚出版的 ...
- springboot系列(三) 启动类中关键注解作用解析
一.Springboot:请求入口 @SpringBootApplication @EnableAspectJAutoProxy @EnableScheduling @EnableTransactio ...
- java - day010 - 基本类型包装,自动装箱和拆箱,日期,集合
基本类型的包装类 byte Byte short Short int Integer long Long float Float double Double char Character boolea ...
- 自定义View-----汽泡效果
先来看一下这次要实现的最终效果: 首先来实现效果一,为实现效果二做充足的准备,下面开始: 新建工程,并定义一个自定义View,然后将其定义在布局文件中,里面是空实现,之后会一步步来填充代码: MyRi ...
- Spring-Spring配置-依赖注入
5.Spring配置 5.1.别名 <!--别名,如果添加了别名,我们也可以使用别名获取到这个对象--> <alias name="user" alias=&qu ...
- nginx中ngx_http_gzip_module模块
⽤用gzip⽅方法压缩响应数据,节约带宽gzip on;gzip_min_length 1000;gzip_proxied expired no-cache no-store private auth ...
- vs2008重置方法
开始->Microsoft Visual Studio 2008->Visual Studio Tools->Visual Studio 2008 命令提示 然后依次键入如下命令: ...
- 模块讲解---os模块,sys模块,json和pickle模块,logging模块
目录 模块的用法 os模块 常用的功能 sys模块 常用的功能 json和pickle模块 4. logging模块 模块的用法 通过 import 或者from......import...... ...
- 第八章 用SQL语句操作数
--切换数据库:手动切换和命令切换 use MySchool --向Student表中插入数据 --语法:INSERT [INTO] 表名 (列名) VALUES (值列表) --注意事项: --1. ...