简体   繁体   English

如何在Qt中绘制模糊的透明背景

[英]How to draw blurred transparent background in Qt

I have a custom QGraphicsItem class in which I have overriden paint event as below 我有一个自定义QGraphicsItem类,在其中重写了绘画事件,如下所示

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. 这样我就可以看到MyRectangle的背景模糊了。

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: 不必重新发明轮子QGraphicsItem支持QGraphicsEffect ,并且在可用的效果中是QGraphicsBlurEffect,因此您只需要使用它:

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. 注意:如果要创建新的效果,正确的方法是从QGraphicsEffect继承并覆盖draw()方法,因此不必创建为每个项目实现相同效果的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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