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()之间才可以生效。

猜你喜欢

转载自blog.csdn.net/sinat_26871259/article/details/81326142