如何将阴影效果应用于QGraphicsItem

How to apply a drop shadow effect to QGraphicsItem?

本文关键字:应用于 QGraphicsItem 阴影      更新时间:2023-10-16

我是QT的新手,我想为我的QGraphicsRectItem画阴影,但它似乎不起作用我在QGraphicsScene 中使用以下代码

scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
QGraphicsDropShadowEffect * effect = new QGraphicsDropShadowEffect();
effect->setBlurRadius(50);
QGraphicsRectItem * item = new QGraphicsRectItem(100,100,50,50);
item->setBrush(QBrush(Qt::cyan));
item->setGraphicsEffect(effect);
scene->addItem(item);

然而,如果我把它应用到我的图形中,这很好。像这个一样查看

 ui->graphicsView->setGraphicsEffect(effect);

我做错了什么?

可能您在本地创建了效果,并且该指针被擦除,因此将效应的声明移动到头文件中。