当前位置:网站首页>Qt绘制图像
Qt绘制图像
2022-04-23 12:10:00 【MechMaster】
1. 例程代码

void Widget::paintEvent(QPaintEvent *event)
{
QPainter painter;
//绘制image
QImage image(100,100,QImage::Format_ARGB32);
painter.begin(&image);
painter.setPen(QPen(Qt::red,3));
painter.setBrush(Qt::yellow);
painter.drawRect(10,10,60,60);
painter.drawText(10,10,60,60,Qt::AlignCenter,tr("QImage"));
painter.setBrush(QColor(0,0,0,100));
painter.drawRect(50,50,40,40);
painter.end();
//绘制pixmap
QPixmap pix(100,100);
painter.begin(&pix);
painter.setPen(QPen(Qt::red,3));
painter.setBrush(Qt::yellow);
painter.drawRect(10,10,60,60);
painter.drawText(10,10,60,60,Qt::AlignCenter,tr("QPixmap"));
painter.setBrush(QColor(0,0,0,100));
painter.drawRect(50,50,40,40);
painter.end();
//绘制bitmap
QBitmap bit(100,100);
painter.begin(&bit);
painter.setPen(QPen(Qt::red,3));
painter.setBrush(Qt::yellow);
painter.drawRect(10,10,60,60);
painter.drawText(10,10,60,60,Qt::AlignCenter,tr("QBitmap"));
painter.setBrush(QColor(0,0,0,100));
painter.drawRect(50,50,40,40);
painter.end();
//绘制picture
QPicture picture;
painter.begin(&picture);
painter.setPen(QPen(Qt::red,3));
painter.setBrush(Qt::yellow);
painter.drawRect(10,10,60,60);
painter.drawText(10,10,60,60,Qt::AlignCenter,tr("QPicture"));
painter.setBrush(QColor(0,0,0,100));
painter.drawRect(50,50,40,40);
painter.end();
//在widget部件上进行绘制
painter.begin(this);
painter.drawImage(50,20,image);
painter.drawPixmap(200,20,pix);
painter.drawPixmap(50,170,bit);
painter.drawPicture(200,170,picture);
}
2. 四个常用的绘图设备
2.1 QImage
- QImage主要用来进行I/O处理,它对I/O处理操作进行了优化,而且可以用来直接访问和操作像素;
2.2 QPixmap
- QPixmap主要用来在屏幕上显示图像,它对在屏幕上显示图像那个进行了优化;
2.3 QBitmap
- QBitmap 是 QPixmap 的子类,用来处理颜色深度为1的图像,即只能显示黑白两种颜色;
2.4 QPicture
- QPicture用来记录并重演QPainter命令。
3. 复合模式

void Widget::paintEvent(QPaintEvent *event)
{
QPainter painter;
QImage image(400,300,QImage::Format_ARGB32_Premultiplied);
painter.begin(&image);
painter.setBrush(Qt::green);
painter.drawRect(100,50,200,200);
painter.setBrush(QColor(0,0,255,15));
painter.drawRect(50,0,100,100);
painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
painter.drawRect(250,0,100,100);
painter.setCompositionMode(QPainter::CompositionMode_DestinationOver);
painter.drawRect(50,200,100,100);
painter.setCompositionMode(QPainter::CompositionMode_Xor);
painter.drawRect(250,200,100,100);
painter.end();
painter.begin(this);
painter.drawImage(0,0,image);
}

版权声明
本文为[MechMaster]所创,转载请带上原文链接,感谢
https://liuhui.blog.csdn.net/article/details/124264238
边栏推荐
- IFLYTEK's revenue in 2021 was 18.3 billion yuan: a year-on-year increase of 41% and a net profit of 1.556 billion yuan
- What is a gateway
- IDEA 代码格式化插件Save Actions
- 第二十六课 类的静态成员函数
- How the database fills in IM expressions (IM 5.4)
- IMEU如何与IMCU相关联(IM 5.5)
- 激活函数之relu函数
- 为什么要有包装类,顺便说一说基本数据类型、包装类、String类该如何转换?
- Idea setting copyright information
- Tclerror: no display name and no $display environment variable
猜你喜欢

5个免费音频素材网站,建议收藏
![[web daily practice] eight color puzzle (float)](/img/59/474080f6377b3684aa4fb2a39e1d81.png)
[web daily practice] eight color puzzle (float)

IDEA 中 .properties文件的中文显示乱码问题的解决办法

如果你是一个Golang面试官,你会问哪些问题?

IDEA 数据库插件Database Navigator 插件

Pagoda panel command line help tutorial (including resetting password)

Idea setting copyright information

IDEA 代码质量规范插件SonarLint

AI 视频云 VS 窄带高清,谁是视频时代的宠儿

How to expand the capacity of the server in the 100 million level traffic architecture? Well written!
随机推荐
worder字体网页字体对照表
Lesson 26 static member functions of classes
用户接口和IM表达式(IM 5.6)
The database navigator uses the default MySQL connection prompt: the server time zone value 'Ö Ð¹ ú±ê ×¼ ʱ ¼ ä’ is unrecognized or repres
Solution of asynchronous clock metastability -- multi bit signal
Fabric 1.0源代码分析(33) Peer #peer channel命令及子命令实现
为什么hash%length==hash&(length-1)的前提是 length 是 2 的 n 次方
Fabric 1.0 source code analysis (33) implementation of peer channel command and subcommand
Master slave replication configuration of MySQL
Why is the premise of hash% length = = hash & (length-1) that length is the nth power of 2
AI video cloud vs narrowband HD, who is the darling of the video era
Interpretation 3 of gdpr series: how do European subsidiaries return data to domestic parent companies?
C# F23.StringSimilarity库 字符串重复度、文本相似度、防抄袭
软件测试基础DAY2-用例执行
WIN10 启动后花屏
C set Logo Icon and shortcut icon
什么是网关
第二十四课 经典问题解析
On using go language to create websocket service
IM表达式如何工作(5.3)