【问题标题】:Share the same QGraphicsEffect for many QGraphicsItems为许多 QGraphicsItem 共享相同的 QGraphicsEffect
【发布时间】:2013-12-15 05:25:41
【问题描述】:

我做什么:

我正在我的视图 (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);

我希望所有这些 QGraphicsItem 共享相同的 QGraphicsBlurEffect,以便我可以通过调用轻松切换启用/禁用

m_blurEffect->setEnabled(false); //(true)

在视图中(他们从那里获得指向效果的指针。


它的作用:

在创建进度之后,这些项目中似乎只有一项包含指针。 在创建新的 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* 对象的正确引用。

如有需要,我可以提供更多细节。

【问题讨论】:

    标签: c++ qt opengl reference qgraphicsitem


    【解决方案1】:

    QGraphicsEffects 不能在多个图形项之间共享。您需要为每个图形项创建一个图形效果对象。见QGraphicsItem::setGraphicsEffect的文档:

    如果 effect 安装在不同的项目上,setGraphicsEffect() 将从项目中删除效果并将其安装在此项目上。 QGraphicsItem 拥有效果

    【讨论】:

      猜你喜欢
      • 2014-07-02
      • 2017-06-11
      • 2017-03-06
      • 1970-01-01
      • 1970-01-01
      • 2017-01-17
      • 2020-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多