1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QPainter>
  4. #include <QFont>
  5.  
  6. Widget::Widget(QWidget *parent) :
  7. QWidget(parent),
  8. ui(new Ui::Widget)
  9. {
  10. x = ;
  11. ui->setupUi(this);
  12.  
  13. connect(ui->move, &QPushButton::clicked, this, [=]()
  14. {
  15. // 刷新窗口
  16. update(); // 系统调用paintEvent 函数
  17. });
  18. }
  19.  
  20. Widget::~Widget()
  21. {
  22. delete ui;
  23. }
  24.  
  25. void Widget::paintEvent(QPaintEvent *)
  26. {
  27. // 创建画家类对象
  28. QPainter p(this); // 指定绘图设备
  29.  
  30. #if 0
  31. // 创建新画笔 -- 轮廓
  32. QPen pen;
  33. pen.setColor(/*Qt::green*/QColor(, , ));
  34. pen.setWidth(); // 像素
  35. pen.setStyle(Qt::DotLine);
  36.  
  37. // 闭合区域使用画刷
  38. QBrush brush(QPixmap(":/Image/face.png"));
  39. p.setBrush(brush);
  40.  
  41. // 将新画笔设置给画家类
  42. p.setPen(pen);
  43. // 画背景图
  44. p.drawPixmap(, , QPixmap(":/Image/xks.png"));
  45.  
  46. // 画直线
  47. p.drawLine(QPoint(, ), QPoint(, ));
  48.  
  49. // 画椭圆
  50. p.drawEllipse(QPoint(, ), , );
  51.  
  52. // 画矩形
  53. p.drawRect(, , , );
  54.  
  55. // 写字
  56. QFont font("华文彩云", , , true);
  57. p.setFont(font);
  58. p.drawText(, , "我是中国人, 我爱我的祖国!!!");
  59.  
  60. int width = this->width();
  61. int heght = this->height();
  62. #endif
  63. // 提供笑脸
  64. x += ;
  65. if(x > this->width())
  66. {
  67. x = ;
  68. }
  69. p.drawPixmap(x, , QPixmap(":/Image/sunny.png"));
  70.  
  71. }
  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3.  
  4. #include <QWidget>
  5.  
  6. namespace Ui {
  7. class Widget;
  8. }
  9.  
  10. class Widget : public QWidget
  11. {
  12. Q_OBJECT
  13.  
  14. public:
  15. explicit Widget(QWidget *parent = );
  16. ~Widget();
  17.  
  18. protected:
  19. /*
  20. * 1. 回调函数
  21. * 2. 此函数不需要用户与调用, 再刷新的时候会自定调用
  22. * 1. 窗口显示的时候
  23. * 2. 最大化, 最小化
  24. * 3. 窗口被这遮挡, 重新显示的时候
  25. * 4. 用户强制刷新的时候
  26. * 5. ...........
  27. * 3. 如果想使用画家类在窗口中画图, 操作必须在paintEvent函数中完成
  28. */
  29. void paintEvent(QPaintEvent *);
  30.  
  31. private:
  32. Ui::Widget *ui;
  33. int x;
  34. };
  35.  
  36. #endif // WIDGET_H

QPixmap,QImage,QPicture

  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. #include <QPainter>
  4. #include <QPicture>
  5.  
  6. Widget::Widget(QWidget *parent) :
  7. QWidget(parent),
  8. ui(new Ui::Widget)
  9. {
  10. ui->setupUi(this);
  11.  
  12. // 在QPixmap中画图
  13. QPixmap pix(, ); // 纸的大小
  14. pix.fill(Qt::red);
  15. QPainter p(&pix);
  16. p.setPen(QPen(Qt::green, ));
  17. p.drawRect(, , , );
  18. p.drawEllipse(, , , );
  19. pix.save("D:\\mypixmap.png");
  20.  
  21. // 指定绘图设备 1. 构造函数中(参数是绘图设备)
  22. // 2. begin(参数是绘图设备)
  23. // end();
  24. // 在QImage中画图
  25. QImage img(, , QImage::Format_RGB32); // 纸的大小
  26. img.fill(Qt::red);
  27. p.begin(&img);
  28. p.setPen(QPen(Qt::green, ));
  29. p.drawRect(, , , );
  30. p.drawEllipse(, , , );
  31. p.end();
  32. img.save("D:\\myImage.png");
  33.  
  34. // 在QPicture中画图
  35. // 1. 保存的是绘图步骤 -- 画家类
  36. // 2. 不是图片, 二进制文件(save保存生成的文件)
  37. // 3. 不依赖平台
  38. QPicture pic; // 纸的大小
  39. p.begin(&pic);
  40. p.setPen(QPen(Qt::green, ));
  41. p.drawRect(, , , );
  42. p.drawEllipse(, , , );
  43. p.end();
  44. pic.save("D:\\mypic.aaa");
  45. }
  46.  
  47. // QWidget
  48. // QPixmap QImage QPicture QBitmap(黑白图片)
  49. // QBitmap 父类 QPixmap
  50. // QPixmap -- 图片类, 主要用来显示, 它针对于显示器显示做了特殊优化, 依赖于平台的, 只能在主线程中使用(UI线程)
  51. // QIamge -- 图片类 , 不依赖有平台, (图片传输 , 可以在多线程中对其进行操作)
  52. Widget::~Widget()
  53. {
  54. delete ui;
  55. }
  56.  
  57. void Widget::paintEvent(QPaintEvent *)
  58. {
  59. QPainter p(this);
  60. QPicture pic;
  61. pic.load("D:\\mypic.png");
  62. p.drawPicture(, , pic);
  63. }
  1. QPixmap:
  2. p.load("路径");———加载图片
  3. p.drawPixmap(b);
  4. a.专门为图像在屏幕上的显示做了优化,依赖于平台
  5. b.主要应用于平台上的图形显示,在不同的平台上拥有相同是显示效果
  6. QBitmap b;--
  7. p.drawPixmap(b)
  8. QPixmap的一个子类,只显示黑白色
  9. QImage
  10. a.使用独立于硬件的绘制系统,专门为图像的像素级访问做了优化
  11. b.可以在多线程中使用
  12. c.可以修改图片中的任意一个像素值
  13. QPicture -- 二进制文件
  14. a.记录和重现QPainter 的各种命令
  15. b.与平台无关
  16.  
  17. QPixmap QImage 的直接相互转换
  18. .QPixmap-->QImage:QPixmap::toImage()
  19. .QImage-->QPixmap:QPixmap::fromImage() [static]

(十一)QPainter绘图, QPixmap,QImage,QPicture,QBitmap的更多相关文章

  1. Qt笔记——绘图(QBitmap,QPixmap,QImage,QPicture)

    QPainter绘图 重写绘图事件,虚函数 如果窗口绘图,必须放在绘图事件里实现 绘图事件内部自动调用,窗口需要重绘的时候,状态改变 绘图设备(QPixmap,QImage,QBitmap,QPict ...

  2. Qt 绘图(QBitmap,QPixmap,QImage,QPicture)

    QPainter绘图绘图设备(QPixmap,QImage,QBitmap,QPicture) 重写绘图事件,虚函数 如果窗口绘图,必须放在绘图事件里实现 绘图事件内部自动调用,窗口需要重绘的时候,状 ...

  3. [Qt2D绘图]-05绘图设备-QPixmap&&QBitmap&&QImage&&QPicture

    这篇笔记记录的是QPainterDevice(绘图设备,可以理解为一个画板) 大纲:     绘图设备相关的类:QPixmap QBitmap QImage QPicture     QPixmap ...

  4. QPixmap QImage 相互转化

    QPainter p(this); QPixmap pixmap; pixmap.load("E:\\参考文件\\image\\1.jpg"); //QPixmap->QIm ...

  5. QPixmap,QImage图片大小缩放linux版

    注意事项: 1.装载图片的地址有时候会读取不到.可以多摸索一下当前系统对应的格式. 2.scaled缩放方式选择 3.注意保存路径.下面程序保存路径是当前执行文件目录中. PicOpera::PicO ...

  6. Qt QPixmap QImage 图片等比例缩放到指定大小

    QPixmap pixmap(path); //pixmap=QPixmap::fromImage(imgShow); pixmap = pixmap.scaled(, , Qt::KeepAspec ...

  7. Qt开发中的实用笔记一--xml,Qpainter,Delegate:

    因为开发环境不能联网,开发中用到有用的知识就记在word稳定中,不知不觉就记载了几十页,为避免笔记丢失,现在就一点点忘博客上搬,方便日后回顾! ---------------------------- ...

  8. qt之图像处理

    毕业2年了,一直使用的qt做桌面程序,很少接触图像算法类的东西,最近由于项目的原因,不得不了解下图像处理,不过也是一些简单的图像处理,仅此作为记录,并希望能帮助初学qt图像处理的朋友. 首先我推荐一篇 ...

  9. Imagelab-0-QT label显示 opencv 图像

    Imagelab-0-QT label显示 opencv 图像 opencvc++qtimagelab 开始之前 这其实也是opencv 处理图像的系列, 只是想我们在进一步复杂化我们的代码之前, 每 ...

随机推荐

  1. OpenCL中三种内存创建image的效率对比

    第一种:使用ION: cl_mem_ion_host_ptr ion_host_ptr1; ion_host_ptr1.ext_host_ptr.allocation_type = CL_MEM_IO ...

  2. layui+ztree 树状下拉框

    一.效果图 [关闭] [展开] 二.代码 [HTML]注:布局一定要用DIV不是select否则效果···· <div class="layui-form-item"> ...

  3. vue 外部字体图标使用,无须绝对路径引入办法

    通常外部字体图标都在使用 iconfont ,这种图标在网上搜到一大把都是由于路径问题显示不出来,或者是显示个方块. 最近的项目中也碰到这个坑爸的问题,总结一下解决办法: 和 webpack.conf ...

  4. PMM Client 安装异常报错

    1.PMM架构 如下图所示 2.Client主要组件 PMM Client是安装在你要监视的MySQL或MongoDB主机上的一组代理组件.组件收集关于一般系统和数据库性能的各种数据,并将该数据发送到 ...

  5. vue框架构建项目流程

    构建项目流程: 1.全局查询:node -v 2.全局初始化:npm install --global vue-cli 3.模块化工程:vue init webpack myapp--->y,n ...

  6. C# 霍尼韦尔扫码枪扫码打印

    程序运行背景条件: 1.将扫码枪调制串口驱动模式 2.将扫码枪所在串口拆分成几个虚拟串口 3.扫码枪扫描条码就打印条码 4.WinForm程序 条码控件使用 DevExpress.XtraEditor ...

  7. Golang 入门 : 打造开发环境

    工欲善其事,必先利其器!在学习和使用 Golang 时如果有一款得心应手的 IDE,相信一定可以事半功倍.虽然很多 IDE 都提供了对 Golang 的支持,但真正好用的没几个.VSCode 算是不错 ...

  8. 基于 docker 的redis 主从+哨兵(快速部署)

    很简单(字多的步骤见:http://www.cnblogs.com/vipzhou/p/8580495.html) 1.直接启动3个容器 docker network create --subnet ...

  9. 重写URL

    更新网页url /***省略部分代码***/ function rewriteUrl(url) { if (url.substr(0, 1) === "/") { url = (r ...

  10. ERP常见问题总结处理

    1. ERP系统的重量录入组件设计不合理易导致录入错误 如下图所示: 修正方法: 1. 更正数据 使用SQL语句更正数据,已经生产完成,作为单据重新录入比较麻烦 UPDATE e_wms_materi ...