Qt 画图工具擦除操作,恢复透明色

在以transparent填充的QPixmap上用红色画笔画出了线,现需要擦除部分红色,恢复出原来的透明色。

使用QPainter::CompositionMode 图像叠加模式

下图简单示意了10种模式:

在Qt的官方文档里我们也找到了具体模式的解释

Constant

Value

Description

QPainter::CompositionMode_SourceOver

0

This is the default mode. The alpha of the source is used to blend the pixel on top of the destination.

QPainter::CompositionMode_DestinationOver

1

The alpha of the destination is used to blend it on top of the source pixels. This mode is the inverse of CompositionMode_SourceOver.

QPainter::CompositionMode_Clear

2

The pixels in the destination are cleared (set to fully transparent) independent of the source.

QPainter::CompositionMode_Source

3

The output is the source pixel. (This means a basic copy operation and is identical to SourceOver when the source pixel is opaque).

QPainter::CompositionMode_Destination

4

The output is the destination pixel. This means that the blending has no effect. This mode is the inverse of CompositionMode_Source.

QPainter::CompositionMode_SourceIn

5

The output is the source, where the alpha is reduced by that of the destination.

QPainter::CompositionMode_DestinationIn

6

The output is the destination, where the alpha is reduced by that of the source. This mode is the inverse of CompositionMode_SourceIn.

QPainter::CompositionMode_SourceOut

7

The output is the source, where the alpha is reduced by the inverse of destination.

QPainter::CompositionMode_DestinationOut

8

The output is the destination, where the alpha is reduced by the inverse of the source. This mode is the inverse of CompositionMode_SourceOut.

QPainter::CompositionMode_SourceAtop

9

The source pixel is blended on top of the destination, with the alpha of the source pixel reduced by the alpha of the destination pixel.

QPainter::CompositionMode_DestinationAtop

10

The destination pixel is blended on top of the source, with the alpha of the destination pixel is reduced by the alpha of the destination pixel. This mode is the inverse of CompositionMode_SourceAtop.

QPainter::CompositionMode_Xor

11

The source, whose alpha is reduced with the inverse of the destination alpha, is merged with the destination, whose alpha is reduced by the inverse of the source alpha. CompositionMode_Xor is not the same as the bitwise Xor.

 

QPainter::CompositionMode_Clear可以清除并且以透明色填充,正式我们所需要的模式。

/* 
   鼠标左键为画图,右键为擦除
   int leftorright 表示鼠标左右键
   leftorright = 1;    //左键被按下
   leftorright = 2;    //右键被按下  
*/
    QPainter *painter = new QPainter;            //新建一个QPainter对象
    QPen pen;                                    //新建一个QPen对象
   //设置画笔的线型,style表示当前选择的线型是Qt::PenStyle枚举数据中的第几个元素
    pen.setStyle ((Qt::PenStyle)1);    
    if (leftorright == 1)                              
    {    
        pen.setWidth (2);                              //设置画笔的线宽值
        pen.setColor (Qt::red);                        //设置画笔的颜色
        painter -> begin(pix);
        //画图时使用CompositionMode_DestinationOver模式
        painter -> setCompositionMode(QPainter::CompositionMode_DestinationOver);
        painter -> setPen(pen);                       //将QPen对象应用到绘制对象当中
        //绘制从startPos到鼠标当前位置的直线
        painter -> drawLine(startPos, e->pos());
        painter -> end();                             //绘制成功返回true
    }
    else if (leftorright == 2)                        
    {
        pen.setWidth(5);                              //在擦除时画笔稍粗一些
        painter -> begin(pix);
        //设置为clear模式,用透明色擦除
        painter -> setCompositionMode(QPainter::CompositionMode_Clear);
        painter -> setPen(pen);                       //将QPen对象应用到绘制对象当中
        //绘制从startPos到鼠标当前位置的直线
        painter -> drawLine(startPos, e->pos());
        painter -> end();                             //绘制成功返回true
    }
    /***
     * 以QPixmap对象为QPaintDevice参数绘制,构造一个QPainter对象,
     * 就立即开始对绘画设备进行绘制,此构造QPainter对象是短期的
     * 由于当一个QPainter对象的初始化失败时构造函数不能提供反馈信息,
     * 所以在绘制 外部设备时 应使用begin()和end()(Ps:如打印机外部设备)
     */
    startPos = e -> pos();                        //更新鼠标的当前位置,为下次绘制做准备
    update();                                     //重绘绘制区窗体

问题:

在运行时控制台一直输出:QPainter::setCompositionMode: Painter not active

这表明对QPainter的操作没有生效。

解决:

对QPainter对象的操作应全部放在QPainter对象begin()与end()之间才可以生效。

  • 5
    点赞
  • 60
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
FileUtil是一个基于Qt框架开发的文件操作工具Qt是一种跨平台的C++应用程序开发框架,可以实现图形用户界面(GUI)以及许多其他功能。 FileUtil提供了许多方便的文件操作功能,可以在不同的操作系统上使用。它可以帮助我们进行文件的创建、复制、移动、重命名、删除以及文件夹的创建和删除等操作。 使用FileUtil,我们可以很方便地在程序中读取和写入文件。我们可以打开一个文件,并通过读取和写入流来读取或写入文件的内容。FileUtil还提供了一些常用的函数,比如判断文件是否存在、获取文件大小,读取和写入文件时的进度等。 除了文件操作,FileUtil还可以进行文件夹的操作。它可以帮助我们创建、删除文件夹,以及遍历文件夹中的文件和子文件夹。 FileUtil还提供了一些其他的功能,比如文件的压缩和解压缩,文件权限的设置等。 作为一个基于Qt的文件操作工具,FileUtil具有跨平台的优势。无论我们是在Windows、Mac还是Linux系统上开发应用程序,都可以使用FileUtil来进行文件操作,而无需关心不同操作系统的差异。 总而言之,FileUtil是一个方便实用的文件操作工具,为我们提供了简单易用的文件操作接口。无论是读取、写入文件,还是进行文件夹操作,FileUtil都能满足我们的需求。使用它,我们可以更方便地对文件进行管理和操作

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值