GUI-Novice
GUI-Novice

Reputation: 361

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.

Upvotes: 0

Views: 2606

Answers (1)

eyllanesc
eyllanesc

Reputation: 244162

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:

enter image description here

enter image description here

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.

Upvotes: 2

Related Questions