Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Get item from QGraphicScene



  • Hi,

    In QGtaphicsScene I add few items. Then I use the flag QGraphicsItem::ItemSendsGeometryChanges and when the item is moved the program goes to the QVariant 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...
    1768eb7f-8f47-47ba-a56d-5fb75d12a9dc-image.png


  • Qt Champions 2019

    Use a debugger and see where it crashes. I would guess scene() returns a nullptr.


  • Qt Champions 2019

    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 in itemChange 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!


Log in to reply