Search code examples
c++qtqgraphicsview

Does a QGraphicsView take ownership over its associated graphics scene?


I was wondering... if I allocate a graphics scene

QGraphicsScene* scene = new QGraphicsScene();

and associate it with a graphics view

this->ui->graphicsView->setScene(scene);

does the graphics view take ownership of the scene? In other words, does the graphics view delete the scene in its destructor or should I delete the scene myself?


Solution

  • The answer is no.
    That's because Qt makes it possible to display one model (QGraphicsScene in this case) in many views which is a standard feature of every model/view framework.

    Documentation of QGrahpicsView::setScene() lacks information about what happens to ownership of a scene but the situation is the same as with others views; for example from documentation of void QWebView::setPage ( QWebPage * page )

    The parent QObject of the provided page remains the owner of the object. If the current document is a child of the web view, it will be deleted.