我的工作:
我正在我的 View (QMainWindow) 的构造函数中创建一个新的 QGraphicsEffect:
QGraphicsBlurEffect *m_blurEffect = new QGraphicsBlurEffect(this);
QGraphicsScene *m_scene = new GameGraphicsScene();
QGraphicsView *m_graphicsView = new QGraphicsView();
m_graphicsView->setScene(m_scene);
然后我创建了几个QGraphicsItems:
QGraphicsItem *item = new QGraphicsItem;
item->setGraphicsEffect(m_blurEffect); //<< This Line is where I need help
m_scene->addItem(item);
我希望所有这些 QGraphicsItems 共享相同的 QGraphicsBlurEffect,这样我就可以通过调用轻松切换启用/禁用
m_blurEffect->setEnabled(false); //(true)
在 View 中(他们从中获得指向效果的指针。
它的作用:
看起来这些项目中只有一个包含指针,在创建进度之后。 在创建新的 QGraphicsItem 后,我使用 qDebug 向我展示指向 m_blurEffect 的指针是否正确
for(...)
{
QGraphicsItem *item= new TileGraphicsItem();
item->setGraphicsEffect(m_blurEffect);
qDebug() << m_blurEffect << tileItem->graphicsEffect();
}
输出是正确的,类似于:
qDebug(): [...]
QGraphicsBlurEffect(0x139fe120) QGraphicsBlurEffect(0x139fe120)
QGraphicsBlurEffect(0x139fe120) QGraphicsBlurEffect(0x139fe120)
QGraphicsBlurEffect(0x139fe120) QGraphicsBlurEffect(0x139fe120)
QGraphicsBlurEffect(0x139fe120) QGraphicsBlurEffect(0x139fe120)
[...]
为了进行更多检查,当我将鼠标悬停在项目上时,我让 qDebug 再次告诉我对 m_blurEffect 的引用。 结果显示,它们都是空指针
qDebug() << graphicsEffect();
QObject(0x0)
除了创建的最后一个项目,它包含正确的引用(在我的例子中是 QGraphicsBlurEffect(0x139ce140))。
那我能做什么呢?
我想实现的是,它们都共享对我在它们存在之前创建的一个 QGraphicsEffect* 对象的正确引用。
如有必要,我可以提供更多详细信息。
最佳答案
QGraphicsEffect
不能在多个图形项之间共享。您需要为每个图形项创建一个图形效果对象。请参阅 QGraphicsItem::setGraphicsEffect
的文档:
If effect is the installed on a different item, setGraphicsEffect() will remove the effect from the item and install it on this item. QGraphicsItem takes ownership of effect.
关于c++ - 为多个 QGraphicsItem 共享同一个 QGraphicsEffect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20576615/