Important: Please read the Qt Code of Conduct -

Cannot save the contents of a QGraphicsScene in QGraphicsView

  • I want my application to save the contents of my scene which contains graphics items in it and later be able to retrieve all those items(not as a QPixmap, but as actual individual items).
    I have tried using Qsettings but get a QVariant error.

    QSettings settings("AdvProgLab","ERapp");
    settings.setValue("saved_scene",scene);      //Here, scene is a QGraphicsScene

    And the error which I am getting is:

        qvariant.h:471: error: ‘QVariant::QVariant(void*)’ is private inline QVariant(void *) Q_DECL_EQ_DELETE; ^
        error: use of deleted function ‘QVariant::QVariant(void*)’

    Can anybody please explain me where I am going wrong ?
    Or is there any other way by which I can save the QGraphicsItems and later retrieve them.

    Thank You.

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You are wrong with your assumption. From what you wrote, you are basically trying to save a pointer to a scene in your settings. If you want to store the content of the scene, it's up to you to serialise your items in a reproducible way. i.e. save each item's position and content as well as properties you are interested in and then load them back on start.

  • @SGaist I understood what you said but i have one doubt regarding your reply.
    Suppose i have few QGraphicsRectItem and QgraphicsEllipseItem in my scene, according to you i should save the position and size of each item but while retrieving, how will the scene know whether it was a RectItem or and EllipseItem ?

    I am new to QT so pardon me if i sound silly.
    Thank You.

  • Lifetime Qt Champion

    You have to store enough information about your items in order to rebuild them when you load the information back.

Log in to reply