Get item from QGraphicScene
-
Hi,
In
QGtaphicsScene
I add few items. Then I use the flagQGraphicsItem::ItemSendsGeometryChanges
and when the item is moved the program goes to theQVariant GraphicVertical::itemChange ( GraphicsItemChange change, const QVariant & value )
function.
Here I need to use either QGraphicsScene::items() or QGraphicsScene::itemAt()
When I use any of these:QList<QGraphicsItem*> items = this->scene()->items(Qt::DescendingOrder);
or
QGraphicsItem *item = this->scene()->itemAt(20, 20, QTransform());
I get an exception error (in debug mode): (why?)
Application was stopped by the exception...
-
Use a debugger and see where it crashes. I would guess scene() returns a nullptr.
-
@Christian-Ehrlicher yes! As you said this->scene() returnes zero
but why? How to overcome it?
EDIT
I understood now.
The scene() appears after some operations and as I write my code initemChange
I simply need to write:if (this->scene()){ // if the scene() exist then do the following QGraphicsItem *item = this->scene()->itemAt(mapToScene(20, 20), QTransform()); QList<QGraphicsItem*> items = this->scene()->items(Qt::DescendingOrder); }
So thank you! you solved my problem!