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

How to clone a QGraphicsItem object?



  • Hi,

    Maybe this question is too obvious but I really appreciate any help. What is the best way to make an independent copy of a QGraphicsItem object?

    QGraphicsPathItem *original = new QGraphicsPathItem;
    original->setPath(originalPath);
    
    QGraphicsPathItem *itemCopy = ???
    

    I want to manipulate the object copy without modifying/altering the original item.

    Any hint? :)


  • Lifetime Qt Champion

    hi
    If you look in source of QGraphicsItem you will see

    private:
    Q_DISABLE_COPY(QGraphicsItem)
    Q_DECLARE_PRIVATE(QGraphicsItem)

    So it is not allowed. ( as far as i know)

    So you can not copy or clone it as such.

    Only option is to create a new item and then copy the data to the new instance.


Log in to reply