1. 程式人生 > >Qt 畫圖工具擦除操作,恢復透明色

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