c++ - Qt中如何绘制模糊的透明背景

标签 c++ qt qt5

我有一个自定义的 QGraphicsItem 类,我在其中重写了绘制事件,如下所示

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

    //code to fill blured background fill
}

我需要用模糊透明效果填充录制区域。这样我就可以看到 MyRectangle 的背景是模糊的。

最佳答案

没有必要重新发明轮子QGraphicsItem支持QGraphicsEffect , 在这些效果中可用的是 QGraphicsBlurEffect所以你只需要使用它:

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

输出:

enter image description here

enter image description here

注意:如果你想创建新的效果,正确的方法是继承QGraphicsEffect并覆盖draw()方法,所以没有必要创建一个为每个项目实现相同效果的类。

关于c++ - Qt中如何绘制模糊的透明背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48706480/

相关文章:

c++ - Node-ffi 绑定(bind)到以 NULL 结尾的 C 字符串数组,但得到 `Segmentation fault: 11`

c++ - 读取 2 个 radio 、串行通信之间发送的信号

c++ - 无法用QT编译C++

c++ - 在文本文件的特定行和列上插入值

c++ - Qt5 在关闭时崩溃

c++ - BOOST_PP_SEQ_SIZE 如何工作?

c++ - qmake : Build library without the symlinks and 'lib' prefix

c++ - 加密/解密 SQLite 数据库并使用它 "on the fly"

c++ - LibPrivoxy : unresolved external symbol __declspec(dllimport) int __stdcall StartPrivoxy(char *)

c++ - 谁能解释一下这个图像分辨率设置代码?这到底是怎么回事