当前位置:网站首页>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
边栏推荐
- 第二十五课 类的静态成员变量
- 抓包整理————tcp 协议[八]
- Chapter 4: enable and disable im column storage for materialized view (IM 4.6)
- PSCP basic usage
- The maximum number of remote desktop servers has been exceeded
- thinkphp 添加图片文字水印生成带二维码的推广海报
- Resolution due to AMD not found_ ags_ x64. DLL, unable to continue code execution. Reinstallation of the program may solve this problem, Forza horizon 5
- Application of remote integrated monitoring system in power distribution room in 10kV prefabricated cabin project
- The listing of saiweidian Technology Innovation Board broke: a decrease of 26% and the market value of the company was 4.4 billion
- Xinwangda announced that the price of battery products had been increased, and the investment of "weixiaoli" exceeded 1 billion
猜你喜欢

科创人·派拉软件CEO谭翔:零信任本质是数字安全,To B也要深研用户心智

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

网络信息安全之零信任

远程桌面之终端服务器超出了最大允许连接数解决

PSCP basic usage

In idea Solution to the problem of garbled code in Chinese display of properties file

Running error: unable to find or load the main class com xxx. Application

传统企业如何应对数字化转型?这些书给你答案

Debug Jest test cases in VSCode, debug Jest test cases in VSCode, middle note basedir=$(dirname "$" (echo "$0" sed -e -e, s, \ \, / "-e").

如果你是一个Golang面试官,你会问哪些问题?
随机推荐
oh-my-lotto
激活函数之sigmoid函数
AI video cloud vs narrowband HD, who is the darling of the video era
IDEA设置版权信息
第四章 为物化视图启用和禁用IM列存储(IM 4.6)
软件测试对于减少程序BUG有多大帮助?
Fabric 1.0 source code analysis (33) implementation of peer channel command and subcommand
Fastjson 2 来了,性能继续提升,还能再战十年
Siri gave the most embarrassing social death moment of the year
Force buckle - 70 climb stairs
PSCP 基本使用
《通用数据保护条例》(GDPR)系列解读三:欧洲子公司如何向国内母公司回传数据?
Fabric 1.0源代码分析(33) Peer #peer channel命令及子命令实现
远程访问家里的树莓派(上)
ThinkPHP adds image text watermark to generate promotion poster with QR code
Introduction to metalama 4 Use fabric to manipulate items or namespaces
力扣-1137.第N个泰波那契数
Application of remote integrated monitoring system in power distribution room in 10kV prefabricated cabin project
Chapter 4 is a tutorial on forced filling of in memory objects with IM enabled filling objects (IM 4.7)
激活函数之阶跃函数