(十一)QPainter绘图, QPixmap,QImage,QPicture,QBitmap
- #include "widget.h"
- #include "ui_widget.h"
- #include <QPainter>
- #include <QFont>
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- x = ;
- ui->setupUi(this);
- connect(ui->move, &QPushButton::clicked, this, [=]()
- {
- // 刷新窗口
- update(); // 系统调用paintEvent 函数
- });
- }
- Widget::~Widget()
- {
- delete ui;
- }
- void Widget::paintEvent(QPaintEvent *)
- {
- // 创建画家类对象
- QPainter p(this); // 指定绘图设备
- #if 0
- // 创建新画笔 -- 轮廓
- QPen pen;
- pen.setColor(/*Qt::green*/QColor(, , ));
- pen.setWidth(); // 像素
- pen.setStyle(Qt::DotLine);
- // 闭合区域使用画刷
- QBrush brush(QPixmap(":/Image/face.png"));
- p.setBrush(brush);
- // 将新画笔设置给画家类
- p.setPen(pen);
- // 画背景图
- p.drawPixmap(, , QPixmap(":/Image/xks.png"));
- // 画直线
- p.drawLine(QPoint(, ), QPoint(, ));
- // 画椭圆
- p.drawEllipse(QPoint(, ), , );
- // 画矩形
- p.drawRect(, , , );
- // 写字
- QFont font("华文彩云", , , true);
- p.setFont(font);
- p.drawText(, , "我是中国人, 我爱我的祖国!!!");
- int width = this->width();
- int heght = this->height();
- #endif
- // 提供笑脸
- x += ;
- if(x > this->width())
- {
- x = ;
- }
- p.drawPixmap(x, , QPixmap(":/Image/sunny.png"));
- }
- #ifndef WIDGET_H
- #define WIDGET_H
- #include <QWidget>
- namespace Ui {
- class Widget;
- }
- class Widget : public QWidget
- {
- Q_OBJECT
- public:
- explicit Widget(QWidget *parent = );
- ~Widget();
- protected:
- /*
- * 1. 回调函数
- * 2. 此函数不需要用户与调用, 再刷新的时候会自定调用
- * 1. 窗口显示的时候
- * 2. 最大化, 最小化
- * 3. 窗口被这遮挡, 重新显示的时候
- * 4. 用户强制刷新的时候
- * 5. ...........
- * 3. 如果想使用画家类在窗口中画图, 操作必须在paintEvent函数中完成
- */
- void paintEvent(QPaintEvent *);
- private:
- Ui::Widget *ui;
- int x;
- };
- #endif // WIDGET_H
QPixmap,QImage,QPicture
- #include "widget.h"
- #include "ui_widget.h"
- #include <QPainter>
- #include <QPicture>
- Widget::Widget(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Widget)
- {
- ui->setupUi(this);
- // 在QPixmap中画图
- QPixmap pix(, ); // 纸的大小
- pix.fill(Qt::red);
- QPainter p(&pix);
- p.setPen(QPen(Qt::green, ));
- p.drawRect(, , , );
- p.drawEllipse(, , , );
- pix.save("D:\\mypixmap.png");
- // 指定绘图设备 1. 构造函数中(参数是绘图设备)
- // 2. begin(参数是绘图设备)
- // end();
- // 在QImage中画图
- QImage img(, , QImage::Format_RGB32); // 纸的大小
- img.fill(Qt::red);
- p.begin(&img);
- p.setPen(QPen(Qt::green, ));
- p.drawRect(, , , );
- p.drawEllipse(, , , );
- p.end();
- img.save("D:\\myImage.png");
- // 在QPicture中画图
- // 1. 保存的是绘图步骤 -- 画家类
- // 2. 不是图片, 二进制文件(save保存生成的文件)
- // 3. 不依赖平台
- QPicture pic; // 纸的大小
- p.begin(&pic);
- p.setPen(QPen(Qt::green, ));
- p.drawRect(, , , );
- p.drawEllipse(, , , );
- p.end();
- pic.save("D:\\mypic.aaa");
- }
- // QWidget
- // QPixmap QImage QPicture QBitmap(黑白图片)
- // QBitmap 父类 QPixmap
- // QPixmap -- 图片类, 主要用来显示, 它针对于显示器显示做了特殊优化, 依赖于平台的, 只能在主线程中使用(UI线程)
- // QIamge -- 图片类 , 不依赖有平台, (图片传输 , 可以在多线程中对其进行操作)
- Widget::~Widget()
- {
- delete ui;
- }
- void Widget::paintEvent(QPaintEvent *)
- {
- QPainter p(this);
- QPicture pic;
- pic.load("D:\\mypic.png");
- p.drawPicture(, , pic);
- }
- QPixmap:
- p.load("路径");———加载图片
- p.drawPixmap(b);
- a.专门为图像在屏幕上的显示做了优化,依赖于平台
- b.主要应用于平台上的图形显示,在不同的平台上拥有相同是显示效果
- QBitmap b;--
- p.drawPixmap(b)
- 是QPixmap的一个子类,只显示黑白色
- QImage
- a.使用独立于硬件的绘制系统,专门为图像的像素级访问做了优化
- b.可以在多线程中使用
- c.可以修改图片中的任意一个像素值
- QPicture -- 二进制文件
- a.记录和重现QPainter 的各种命令
- b.与平台无关
- QPixmap 和 QImage 的直接相互转换
- .QPixmap-->QImage:QPixmap::toImage()
- .QImage-->QPixmap:QPixmap::fromImage() [static]
(十一)QPainter绘图, QPixmap,QImage,QPicture,QBitmap的更多相关文章
- Qt笔记——绘图(QBitmap,QPixmap,QImage,QPicture)
QPainter绘图 重写绘图事件,虚函数 如果窗口绘图,必须放在绘图事件里实现 绘图事件内部自动调用,窗口需要重绘的时候,状态改变 绘图设备(QPixmap,QImage,QBitmap,QPict ...
- Qt 绘图(QBitmap,QPixmap,QImage,QPicture)
QPainter绘图绘图设备(QPixmap,QImage,QBitmap,QPicture) 重写绘图事件,虚函数 如果窗口绘图,必须放在绘图事件里实现 绘图事件内部自动调用,窗口需要重绘的时候,状 ...
- [Qt2D绘图]-05绘图设备-QPixmap&&QBitmap&&QImage&&QPicture
这篇笔记记录的是QPainterDevice(绘图设备,可以理解为一个画板) 大纲: 绘图设备相关的类:QPixmap QBitmap QImage QPicture QPixmap ...
- QPixmap QImage 相互转化
QPainter p(this); QPixmap pixmap; pixmap.load("E:\\参考文件\\image\\1.jpg"); //QPixmap->QIm ...
- QPixmap,QImage图片大小缩放linux版
注意事项: 1.装载图片的地址有时候会读取不到.可以多摸索一下当前系统对应的格式. 2.scaled缩放方式选择 3.注意保存路径.下面程序保存路径是当前执行文件目录中. PicOpera::PicO ...
- Qt QPixmap QImage 图片等比例缩放到指定大小
QPixmap pixmap(path); //pixmap=QPixmap::fromImage(imgShow); pixmap = pixmap.scaled(, , Qt::KeepAspec ...
- Qt开发中的实用笔记一--xml,Qpainter,Delegate:
因为开发环境不能联网,开发中用到有用的知识就记在word稳定中,不知不觉就记载了几十页,为避免笔记丢失,现在就一点点忘博客上搬,方便日后回顾! ---------------------------- ...
- qt之图像处理
毕业2年了,一直使用的qt做桌面程序,很少接触图像算法类的东西,最近由于项目的原因,不得不了解下图像处理,不过也是一些简单的图像处理,仅此作为记录,并希望能帮助初学qt图像处理的朋友. 首先我推荐一篇 ...
- Imagelab-0-QT label显示 opencv 图像
Imagelab-0-QT label显示 opencv 图像 opencvc++qtimagelab 开始之前 这其实也是opencv 处理图像的系列, 只是想我们在进一步复杂化我们的代码之前, 每 ...
随机推荐
- OpenCL中三种内存创建image的效率对比
第一种:使用ION: cl_mem_ion_host_ptr ion_host_ptr1; ion_host_ptr1.ext_host_ptr.allocation_type = CL_MEM_IO ...
- layui+ztree 树状下拉框
一.效果图 [关闭] [展开] 二.代码 [HTML]注:布局一定要用DIV不是select否则效果···· <div class="layui-form-item"> ...
- vue 外部字体图标使用,无须绝对路径引入办法
通常外部字体图标都在使用 iconfont ,这种图标在网上搜到一大把都是由于路径问题显示不出来,或者是显示个方块. 最近的项目中也碰到这个坑爸的问题,总结一下解决办法: 和 webpack.conf ...
- PMM Client 安装异常报错
1.PMM架构 如下图所示 2.Client主要组件 PMM Client是安装在你要监视的MySQL或MongoDB主机上的一组代理组件.组件收集关于一般系统和数据库性能的各种数据,并将该数据发送到 ...
- vue框架构建项目流程
构建项目流程: 1.全局查询:node -v 2.全局初始化:npm install --global vue-cli 3.模块化工程:vue init webpack myapp--->y,n ...
- C# 霍尼韦尔扫码枪扫码打印
程序运行背景条件: 1.将扫码枪调制串口驱动模式 2.将扫码枪所在串口拆分成几个虚拟串口 3.扫码枪扫描条码就打印条码 4.WinForm程序 条码控件使用 DevExpress.XtraEditor ...
- Golang 入门 : 打造开发环境
工欲善其事,必先利其器!在学习和使用 Golang 时如果有一款得心应手的 IDE,相信一定可以事半功倍.虽然很多 IDE 都提供了对 Golang 的支持,但真正好用的没几个.VSCode 算是不错 ...
- 基于 docker 的redis 主从+哨兵(快速部署)
很简单(字多的步骤见:http://www.cnblogs.com/vipzhou/p/8580495.html) 1.直接启动3个容器 docker network create --subnet ...
- 重写URL
更新网页url /***省略部分代码***/ function rewriteUrl(url) { if (url.substr(0, 1) === "/") { url = (r ...
- ERP常见问题总结处理
1. ERP系统的重量录入组件设计不合理易导致录入错误 如下图所示: 修正方法: 1. 更正数据 使用SQL语句更正数据,已经生产完成,作为单据重新录入比较麻烦 UPDATE e_wms_materi ...