The crash of delete after QT removeitem in qgraphicsscene and the display residue

Problem description

In the process of using QT’s graphicsview framework to create and interact with user-defined items, you need to remove unnecessary user-defined items. However, after the user-defined items interact with the re implemented rotation and scaling, removeitem () + delete will occasionally cause the error of accessing illegal memory, and even if you only use removeitem (), there will occasionally be residual items in the view.

Solutions

Using setitemindex method of qgraphicsscene (qgraphicsscene:: Noindex);
to disable quick query of index can solve the above problem.

other

For the problem of whether to delete after using removeitem(), the official question has given a clear explanation

The ownership of item is passed on to the caller (i.e., qgraphics scene will no longer delete item when destroyed).
remove the item and all its children from the scene. Ownership of the item is passed to the caller (that is, qgraphicsscene will not delete the item when it is destroyed).

So remember to delete

Read More: