简体   繁体   中英

How to draw blurred transparent background in Qt

I have a custom QGraphicsItem class in which I have overriden paint event as below

void MyRectangle::paint(QPainter *painter, const QStyleOptionGraphicsItem 
*option, QWidget *widget)
{
    QRectF rec = boundingRect();

    //code to fill blured background fill
}

I need to fill the rec area with blured transparent effect. So that I can see the background of MyRectangle as blured.

It is not necessary to reinvent the wheel , QGraphicsItem supports QGraphicsEffect , and within those effects available is QGraphicsBlurEffect so you just have to use it:

QGraphicsBlurEffect *effect = new QGraphicsBlurEffect;
item->setGraphicsEffect(effect);

Outputs:

在此处输入图片说明

在此处输入图片说明

Note: If you want to create new effects a proper way is to inherit from QGraphicsEffect and overwrite the draw() method, so it is not necessary to create a class that implements the same effect for each item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM