自定义委托,继承于,QStyledItemDelegate类,重载Paint()函数,

1、实现在QTableView中绘制 格式字符串

2、实现在QTableView中绘制进度条

3、实现在QTableView中绘制QCheckBox

4、实现在QTableView中绘制星星

5、实现在QTableView中绘制Pixmap图片

1、实现在QTableView中绘制 格式字符串

  1. //重载绘制函数
  2. void DelReconQueue::paint(QPainter *painter, const QStyleOptionViewItem &option,
  3. const QModelIndex &index) const
  4. {
  5. //如果是第2列'病人Id'
  6. if (index.column() == 2)
  7. {
  8. //获得当前项值
  9. int patientId = index.model()->data(index, Qt::DisplayRole).toInt();
  10. //设置'病人Id'格式字符串: P:00000x;6位10进制数,不足补0;
  11. QString text = QString("P:%1").arg(patientId, 6, 10, QChar('0'));
  12. //获取项风格设置
  13. QStyleOptionViewItem myOption = option;
  14. myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
  15. //绘制文本
  16. QApplication::style()->drawItemText ( painter, myOption.rect , myOption.displayAlignment, QApplication::palette(), true,text );
  17. }
  18. else
  19. {
  20. //否则调用默认委托
  21. QStyledItemDelegate::paint(painter, option, index);
  22. }
  23. }

   如果自定义委托继承于QItemDelegate类,绘制字符串,可以直接用QItemDelegate中的drawDisplay();

  1. void TrackDelegate::paint(QPainter *painter,
  2. const QStyleOptionViewItem &option,
  3. const QModelIndex &index) const
  4. {
  5. //保存音轨的列
  6. if (index.column() == durationColumn) {
  7. //获得索引对应Model中的数据
  8. int secs = index.model()->data(index, Qt::DisplayRole).toInt();
  9. //设置时间格式字符串 分:秒
  10. QString text = QString("%1:%2")
  11. .arg(secs / 60, 2, 10, QChar('0'))
  12. .arg(secs % 60, 2, 10, QChar('0'));
  13. //获取项风格设置
  14. QStyleOptionViewItem myOption = option;
  15. myOption.displayAlignment = Qt::AlignRight | Qt::AlignVCenter;
  16. //绘制文本
  17. drawDisplay(painter, myOption, myOption.rect, text);
  18. //如果当前有焦点,就绘制一个焦点矩形,否则什么都不做
  19. drawFocus(painter, myOption, myOption.rect);
  20. } else{
  21. //否则默认
  22. QItemDelegate::paint(painter, option, index);
  23. }
  24. }

2、实现在QTableView中绘制进度条

  1. //重载绘制函数
  2. void DelReconQueue::paint(QPainter *painter, const QStyleOptionViewItem &option,
  3. const QModelIndex &index) const
  4. {
  5. //如果是'已经完成子任务数'
  6. if (index.column() == 9)
  7. {
  8. const QAbstractItemModel *itemModel = index.model();
  9. //获得索引对应Model中的数据
  10. int finishedSubTaskNum = itemModel->data(index, Qt::DisplayRole).toInt();
  11. int subTaskNum = itemModel->data(itemModel->index(index.row(),8), Qt::DisplayRole).toInt();
  12. //进度条的风格选项
  13. QStyleOptionProgressBarV2 *progressBarOption = new QStyleOptionProgressBarV2();
  14. progressBarOption->rect = option.rect;
  15. progressBarOption->minimum = 0;
  16. progressBarOption->maximum = subTaskNum;
  17. progressBarOption->progress = finishedSubTaskNum;
  18. int t = finishedSubTaskNum/subTaskNum;
  19. progressBarOption->text = QString::number(t) + "%";
  20. progressBarOption->textVisible = true;
  21. //绘制进度条
  22. QApplication::style()->drawControl(QStyle::CE_ProgressBar, progressBarOption, painter);
  23. }
  24. else
  25. {
  26. //否则调用默认委托
  27. QStyledItemDelegate::paint(painter, option, index);
  28. }
  29. }

  3、实现在QTableView中绘制QCheckBox

  1. #include <QtGui>
  2. #include <QItemDelegate>
  3. #include <QStyleOptionProgressBarV2>
  4. #include "DelReconQueue.h"
  5. //重载绘制函数
  6. void DelReconQueue::paint(QPainter *painter, const QStyleOptionViewItem &option,
  7. const QModelIndex &index) const
  8. {
  9. if (index.column() == 11)
  10. {
  11. //获取值
  12. bool checked = index.model()->data(index, Qt::DisplayRole).toBool();
  13. //按钮的风格选项
  14. QStyleOptionButton *checkBoxOption = new QStyleOptionButton();
  15. checkBoxOption->state |= QStyle::State_Enabled;
  16. //根据值判断是否选中
  17. if(checked)
  18. {
  19. checkBoxOption->state |= QStyle::State_On;
  20. }
  21. else
  22. {
  23. checkBoxOption->state |= QStyle::State_Off;
  24. }
  25. //返回QCheckBox几何形状
  26. checkBoxOption->rect = CheckBoxRect(option);
  27. //绘制QCheckBox
  28. QApplication::style()->drawControl(QStyle::CE_CheckBox,checkBoxOption,painter);
  29. }
  30. else
  31. {
  32. //否则调用默认委托
  33. QStyledItemDelegate::paint(painter, option, index);
  34. }
  35. }

//生成QCheckBox

  1. QRect DgSystemLog::CheckBoxRect(const QStyleOptionViewItem &viewItemStyleOptions)const
  2. {
  3. //绘制按钮所需要的参数
  4. QStyleOptionButton checkBoxStyleOption;
  5. //按照给定的风格参数 返回元素子区域
  6. QRect checkBoxRect = QApplication::style()->subElementRect( QStyle::SE_CheckBoxIndicator, &checkBoxStyleOption);
  7. //返回QCheckBox坐标
  8. QPoint checkBoxPoint(viewItemStyleOptions.rect.x() + viewItemStyleOptions.rect.width() / 2 - checkBoxRect.width() / 2,
  9. viewItemStyleOptions.rect.y() + viewItemStyleOptions.rect.height() / 2 - checkBoxRect.height() / 2);
  10. //返回QCheckBox几何形状
  11. return QRect(checkBoxPoint, checkBoxRect.size());
  12. }

4、实现在QTableView中绘制自定义类 星星

  1. //重载绘制函数
  2. void StarDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
  3. const QModelIndex &index) const
  4. {
  5. //如果某项数据是星星类型
  6. if (qVariantCanConvert<StarRating>(index.data())) {
  7. //获取该项数据,并转换成StarRating类型
  8. StarRating starRating = qVariantValue<StarRating>(index.data());
  9. //如果有控件被选中,我们就让选中的控件变亮
  10. if (option.state & QStyle::State_Selected)
  11. painter->fillRect(option.rect, option.palette.highlight());
  12. starRating.paint(painter, option.rect, option.palette,
  13. StarRating::ReadOnly);
  14. }
  15. //如果没有控件选中,调用默认委托
  16. else {
  17. QStyledItemDelegate::paint(painter, option, index);
  18. }
  19. }

星星自定义源文件

  1. #ifndef STARRATING_H
  2. #define STARRATING_H
  3. #include <QMetaType>
  4. #include <QPointF>
  5. #include <QVector>
  6. class StarRating
  7. {
  8. public:
  9. enum EditMode { Editable, ReadOnly };
  10. StarRating(int starCount = 1, int maxStarCount = 5);
  11. void paint(QPainter *painter, const QRect &rect,
  12. const QPalette &palette, EditMode mode) const;
  13. QSize sizeHint() const;
  14. int starCount() const { return myStarCount; }
  15. int maxStarCount() const { return myMaxStarCount; }
  16. void setStarCount(int starCount) { myStarCount = starCount; }
  17. void setMaxStarCount(int maxStarCount) { myMaxStarCount = maxStarCount; }
  18. private:
  19. QPolygonF starPolygon;
  20. QPolygonF diamondPolygon;
  21. int myStarCount;
  22. int myMaxStarCount;
  23. };
  24. //让所有模板类型都知道该类,包括QVariant
  25. Q_DECLARE_METATYPE(StarRating)
  26. #endif
  27. #include <QtGui>
  28. #include <math.h>
  29. #include "starrating.h"
  30. const int PaintingScaleFactor = 20;
  31. StarRating::StarRating(int starCount, int maxStarCount)
  32. {
  33. myStarCount = starCount;
  34. myMaxStarCount = maxStarCount;
  35. starPolygon << QPointF(1.0, 0.5);
  36. for (int i = 1; i < 5; ++i)
  37. starPolygon << QPointF(0.5 + 0.5 * cos(0.8 * i * 3.14),
  38. 0.5 + 0.5 * sin(0.8 * i * 3.14));
  39. diamondPolygon << QPointF(0.4, 0.5) << QPointF(0.5, 0.4)
  40. << QPointF(0.6, 0.5) << QPointF(0.5, 0.6)
  41. << QPointF(0.4, 0.5);
  42. }
  43. QSize StarRating::sizeHint() const
  44. {
  45. return PaintingScaleFactor * QSize(myMaxStarCount, 1);
  46. }
  47. void StarRating::paint(QPainter *painter, const QRect &rect,
  48. const QPalette &palette, EditMode mode) const
  49. {
  50. painter->save();
  51. painter->setRenderHint(QPainter::Antialiasing, true);
  52. painter->setPen(Qt::NoPen);
  53. if (mode == Editable) {
  54. painter->setBrush(palette.highlight());
  55. } else {
  56. painter->setBrush(palette.foreground());
  57. }
  58. int yOffset = (rect.height() - PaintingScaleFactor) / 2;
  59. painter->translate(rect.x(), rect.y() + yOffset);
  60. //画笔坐标
  61. painter->scale(PaintingScaleFactor, PaintingScaleFactor);
  62. for (int i = 0; i < myMaxStarCount; ++i) {
  63. if (i < myStarCount) {
  64. painter->drawPolygon(starPolygon, Qt::WindingFill);
  65. } else if (mode == Editable) {
  66. painter->drawPolygon(diamondPolygon, Qt::WindingFill);
  67. }
  68. painter->translate(1.0, 0.0);
  69. }
  70. painter->restore();
  71. }

 5、实现在QTableView中绘制Pixmap图片 ,

详细请看具体例子 Qt-在表格(QTableView)中插入图片

  1. void MyItemDelegate::paint(QPainter * painter,
  2. const QStyleOptionViewItem & option,
  3. const QModelIndex & index) const
  4. {
  5. if(index.column()!=0){
  6. QItemDelegate::paint(painter,option,index);
  7. return;
  8. }
  9. const QAbstractItemModel * model=index.model();
  10. QVariant var=model->data(index,Qt::CheckStateRole);
  11. if(var.isNull()) var=false;
  12. const QPixmap & star=var.toBool()?
  13. favouritePixmap:notFavouritePixmap;
  14. int width=star.width();
  15. int height=star.height();
  16. QRect rect=option.rect;
  17. int x=rect.x()+rect.width()/2-width/2;
  18. int y=rect.y()+rect.height()/2-height/2;
  19. painter->drawPixmap(x,y,star);
  20. }

Qt自定义委托在QTableView中绘制控件、图片、文字的更多相关文章

  1. Qt自定义委托在QTableView中绘制控件、图片、文字(内容比较全)

    自定义委托,继承于,QStyledItemDelegate类,重载Paint()函数, 1.实现在QTableView中绘制 格式字符串 2.实现在QTableView中绘制进度条 3.实现在QTab ...

  2. Qt自定义阴影效果和QOpenGLWidget冲突导致控件不刷新

    Qt5.6.2版本存在这样一个问题(其它版本未测试),当main函数中设置了application.setAttribute(Qt::AA_NativeWindows)(用于使得每个子界面都可以获取w ...

  3. Winform中Picture控件图片的拖拽显示

    注解:最近做了一个小工具,在Winform中对Picture控件有一个需求,可以通过鼠标从外部拖拽图片到控件的上,释放鼠标,显示图片! 首先你需要对你的整个Fom窗口的AllowDrop设置Ture ...

  4. 在Paint事件中绘制控件(边框)

    单纯的自己记录,将来会继续添加,侥幸被大家发现了的话请不要太鄙视... private void panel4_Paint(object sender, PaintEventArgs e) { Con ...

  5. paip.提升用户体验---c++ qt自定义窗体(1)---标题栏的绘制

    源地址:http://blog.csdn.net/attilax/article/details/12343625 paip.提升用户体验---c++ qt自定义窗体(1)---标题栏的绘制 效果图: ...

  6. 在WinForm中使用委托来在其他线程中改变控件的显示

    假设winform中有两个控件: 1.ListView用来显示进度的文本提示,ID:listView_progressInfo 2.ProgressBar用来显示进度,ID:progressBar1 ...

  7. Qt在表格中加入控件

    任务:使用QTableWidget动态生成表格,在每行的某两列中加入QComboBox下拉框控件和QPushButton按钮控件 有添加,删除,编辑功能,每行的按钮可以浏览文件夹并选择文件 1.新建一 ...

  8. WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探

    原文:WPF中自定义的DataTemplate中的控件,在Window_Loaded事件中加载机制初探         最近因为项目需要,开始学习如何使用WPF开发桌面程序.使用WPF一段时间之后,感 ...

  9. Kotlin 第一弹:自定义 ViewGroup 实现流式标签控件

    古人学问无遗力, 少壮工夫老始成.纸上得来终觉浅, 绝知此事要躬行. – 陆游 <冬夜读书示子聿> 上周 Google I/O 大会的召开,宣布了 Kotlin 语言正式成为了官方开发语言 ...

随机推荐

  1. 让一个父级div根据子级div高度而自适应高度

    需求是点击上传的时候进行子级div高度不定,相对来说父级div高度也不能固定,把元素都设置成普通标准流,然后样式可以使用margin内边距或者padding外边距来进行调节 放上代码供参考: .opu ...

  2. Computer Vision_33_SIFT:Object recognition from local scale-invariant features——1999

    此部分是计算机视觉部分,主要侧重在底层特征提取,视频分析,跟踪,目标检测和识别方面等方面.对于自己不太熟悉的领域比如摄像机标定和立体视觉,仅仅列出上google上引用次数比较多的文献.有一些刚刚出版的 ...

  3. springboot系列(三) 启动类中关键注解作用解析

    一.Springboot:请求入口 @SpringBootApplication @EnableAspectJAutoProxy @EnableScheduling @EnableTransactio ...

  4. java - day010 - 基本类型包装,自动装箱和拆箱,日期,集合

    基本类型的包装类 byte Byte short Short int Integer long Long float Float double Double char Character boolea ...

  5. 自定义View-----汽泡效果

    先来看一下这次要实现的最终效果: 首先来实现效果一,为实现效果二做充足的准备,下面开始: 新建工程,并定义一个自定义View,然后将其定义在布局文件中,里面是空实现,之后会一步步来填充代码: MyRi ...

  6. Spring-Spring配置-依赖注入

    5.Spring配置 5.1.别名 <!--别名,如果添加了别名,我们也可以使用别名获取到这个对象--> <alias name="user" alias=&qu ...

  7. nginx中ngx_http_gzip_module模块

    ⽤用gzip⽅方法压缩响应数据,节约带宽gzip on;gzip_min_length 1000;gzip_proxied expired no-cache no-store private auth ...

  8. vs2008重置方法

    开始->Microsoft Visual Studio 2008->Visual Studio Tools->Visual Studio 2008 命令提示 然后依次键入如下命令: ...

  9. 模块讲解---os模块,sys模块,json和pickle模块,logging模块

    目录 模块的用法 os模块 常用的功能 sys模块 常用的功能 json和pickle模块 4. logging模块 模块的用法 通过 import 或者from......import...... ...

  10. 第八章 用SQL语句操作数

    --切换数据库:手动切换和命令切换 use MySchool --向Student表中插入数据 --语法:INSERT [INTO] 表名 (列名) VALUES (值列表) --注意事项: --1. ...