سؤال

What I do:

I am creating a new QGraphicsEffect in the constructor of my View (QMainWindow):

QGraphicsBlurEffect *m_blurEffect = new QGraphicsBlurEffect(this);
QGraphicsScene *m_scene = new GameGraphicsScene();
QGraphicsView *m_graphicsView = new QGraphicsView();
m_graphicsView->setScene(m_scene);

Then I create several QGraphicsItems:

QGraphicsItem *item = new QGraphicsItem;
item->setGraphicsEffect(m_blurEffect); //<< This Line is where I need help
m_scene->addItem(item);

I want all these QGraphicsItems to share the same QGraphicsBlurEffect, so that I can easily toggle enabled/disabled by calling

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

in the View (where they get the pointer to the effect from.


What it does:

It appears that only one of these items contains the pointer, after the creating-progress. I used qDebug to show me if the pointer to m_blurEffect is correct after a new QGraphicsItem is created

for(...)
{
   QGraphicsItem *item= new TileGraphicsItem();
   item->setGraphicsEffect(m_blurEffect);
   qDebug() << m_blurEffect << tileItem->graphicsEffect();
}

The output is correct, something like:

qDebug(): [...]

QGraphicsBlurEffect(0x139fe120) QGraphicsBlurEffect(0x139fe120)

QGraphicsBlurEffect(0x139fe120) QGraphicsBlurEffect(0x139fe120)

QGraphicsBlurEffect(0x139fe120) QGraphicsBlurEffect(0x139fe120)

QGraphicsBlurEffect(0x139fe120) QGraphicsBlurEffect(0x139fe120)

[...]

For more checking I let qDebug tell me the reference to m_blurEffect again, when I'm hovering my mouse over the Items. The result shows, that all of them have empty pointers

qDebug() << graphicsEffect();
QObject(0x0)

except the last item that was created, it contains the right reference (QGraphicsBlurEffect(0x139ce140) in my case).


So what can I do?

I want to achieve, that they all share the correct reference to one QGraphicsEffect* object that I created before they existed.

If necessary I can give further detail.

هل كانت مفيدة؟

المحلول

QGraphicsEffects cannot be shared between several graphics items. You need to create an object of graphics effect for each graphics item. See the documentation of 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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top